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

worldblog發表於2007-12-07
c 刪除檔案中指定的部分內容 (轉)[@more@]

#include
#include
#include

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,".")!=NULL) ) {

 n_filename[0]=';
 o_filename[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 : %dn", 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," count=1;
 }else if ( strstr(line," count=0;
 }else if (count==0){
 fputs(line,fw);
 }
 }
 /* read file by line */ 
 fclose(myFile);
}


 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-989374/,如需轉載,請註明出處,否則將追究法律責任。

相關文章