c 刪除檔案中指定的部分內容

來源:互聯網
上載者:User

#include <stdio.h>
#include <dirent.h>
#include <string.h>

void search_file(char* dir);
void clear_file(char* FileName,char* Filename1);
 DIR *dp;
    struct dirent *dirp;
    char* file;
    FILE * fp=NULL;
    char n_filename[50];
    char o_filename[50];
 char buf[80];
 int pid;
 int status;
 char* logdir;
    char* logbak;
    char  *optarg;
   
int main(char *argv[]){

 logdir="/usr/local/tmp_file/";
 logbak="/usr/local/tmp_file2/";
 printf("Begin now !/n");
 
 search_file(logdir);
 

}

void search_file(char* dir){
 if ((dp = opendir(dir)) == NULL)
     {
      printf("directory %s is not found!/n",logdir);
     }
         /* print err */
    
  while ((dirp = readdir(dp)) != NULL){
      optarg = dirp->d_name;
      /*printf ("%s /n" , optarg);*/
      
     if ((strstr(optarg,".htm")!=NULL) |
       (strstr(optarg,".vbs")!=NULL) |
       (strstr(optarg,".asp")!=NULL) ) {

    n_filename[0]='/0';
    o_filename[0]='/0';
    strcat(o_filename,logdir);
    strcat(o_filename,"/");
    strcat(o_filename,optarg);
       strcat(n_filename,logbak);
       strcat(n_filename,optarg);
       printf("%s /n", o_filename);
       clear_file(o_filename,n_filename);
    /* status = rename(o_filename,n_filename); */
       printf("%s %s rename file status : %d/n", o_filename, n_filename, status );
      }     
     } /* for every log file  */
     closedir(dp);
    
     sleep(20); 
}

void clear_file(char* FileName,char* FileName1){

 int i;
 FILE *myFile=NULL,*fw=NULL;
 char* line;
 int count=0;
 myFile = fopen(FileName,"r");   /* open file */
 fw    = fopen(FileName1,"w");
 if(myFile == NULL)
 {
  printf("No config file myFile,Please config your system first!/n");
  exit(1);
 }
 while( fgets(line,1024,myFile)!=NULL ) {   
   /* printf("line is : %s" , line); */
   if ( strstr(line,"<vbscript")!=NULL){
    count=1;
   }else if ( strstr(line,"</vbscript")!=NULL){
     count=0;
   }else if (count==0){
    fputs(line,fw);
   }
 }
 /* read file by line */ 
 fclose(myFile);
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.