CSDN博客

img hem

c 删除文件中指定的部分内容

发表于2001/6/6 18:02:00  2275人阅读

分类: c/c++

#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);
}


 

0 0

相关博文

我的热门文章

img
取 消
img