linux c 判断文件存在,遍历文件,随机修改文件内容

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 #include<assert.h>
  5 #include<string.h>
  6 #include<dirent.h>
  7 #include<unistd.h>
  8 #include<sys/types.h>
  9 #include<sys/stat.h>
 10 #include<time.h>
 11 
 12 
 13 #define PATH_LEN  512
 14 //#define MODIFICATION_LEN 10
 15 //#define MODIFICATION_TIMES 5
 16 char source_file_folder[]="t5";
 17 char dest_file_folder[]="dest_file";
 18 
 19 int modification(char src_file[],int MODIFICATION_TIMES,int MODIFICATION_LEN,char source_file_dir[],char dest_file_dir[])
 20 {
 21     FILE *src_fp,*dst_fp;
 22     char src_file_path[PATH_LEN];
 23     char dst_file_path[PATH_LEN];
 24     snprintf(src_file_path,PATH_LEN,"%s/%s",source_file_dir,src_file);
 25     snprintf(dst_file_path,PATH_LEN,"%s/%s",dest_file_dir,src_file);
 26     src_fp=fopen(src_file_path,"a+");
 27     assert(src_fp!=NULL);
 28     fseek(src_fp,0,SEEK_END);
 29     int src_file_len=ftell(src_fp);
 30     assert(src_file_len!=-1);
 31     rewind(src_fp);
 32     int *random_offset=(int *)calloc(MODIFICATION_TIMES,sizeof(int));
 33     assert(random_offset!=NULL);
 34     srand((unsigned)time(NULL));
 35     int i=0;
 36     do
 37     {
 38         random_offset[i]=rand()%src_file_len;
 39     }while(random_offset[i++]<=src_file_len && i<MODIFICATION_TIMES);
 40     dst_fp=fopen(dst_file_path,"w+");
 41     assert(dst_fp!=NULL);    
 42     char *buff=(char *)malloc(src_file_len*sizeof(char));
 43     assert(buff!=NULL);
 44     if(fread(buff,src_file_len,1,src_fp)==1)
 45         fwrite(buff,src_file_len,1,dst_fp);
 46     //printf("src file : %s\n",buff);
 47     //free(buff);
 48     i=0;
 49     char *center_buff;
 50     while(i<MODIFICATION_TIMES)
 51     {
 52         int right_offset=random_offset[i]+MODIFICATION_LEN;
 53         right_offset=right_offset>src_file_len?src_file_len:right_offset;
 54         int center_len=right_offset-random_offset[i];
 55         center_buff=(char *)calloc(center_len,sizeof(char));
 56         memset(center_buff,'0',center_len);
 57         fseek(dst_fp,random_offset[i],SEEK_SET);
 58         fwrite(center_buff,center_len,1,dst_fp);    
 59         free(center_buff);
 60         ++i;
 61     }
 62     free(buff);
 63     fclose(src_fp);
 64     fclose(dst_fp);
 65     return 0;
 66 
 67 }
 68 
 69 void listDir(char *path,int modification_times,int modification_len,char source_file_dir[],char dest_file_dir[])
 70 {
 71     DIR *pDir;
 72     struct dirent *ent;
 73     char childpath[PATH_LEN];
 74     pDir=opendir(path);
 75     memset(childpath,0,sizeof(childpath));
 76     while((ent=readdir(pDir))!=NULL)
 77     {
 78         if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
 79             continue;
 80         //    if(ent->d_type==8)
 81         snprintf(childpath,PATH_LEN,"%s/%s",path,ent->d_name);
 82         printf("%s\n",childpath);
 83         modification(ent->d_name,modification_times,modification_len,source_file_dir,dest_file_dir);
 84     }
 85     closedir(pDir);
 86 }
 87 int main(int argc,char *argv[])
 88 {
 89     char ch;
 90     char *source_file_dir=source_file_folder;
 91     char *dest_file_dir=dest_file_folder;
 92     int modification_len=10,modification_times=1;
 93     if(argc<2)
 94     {
 95         printf("Usage:%s -t modification_times -l modification_len -s source_file_folder -d dest_file_folder\n",argv[0]);
 96         return 0;
 97     }    
 98     while((ch=getopt(argc,argv,"t:l:s:d:"))!=-1)
 99     {
100         switch(ch)
101         {
102             case 't':
103                 modification_times=atoi(optarg);
104                 break;
105             case 'l':
106                 modification_len=atoi(optarg);
107                 break;
108             case 's':
109                 source_file_dir=optarg;
110                 break;
111             case 'd':
112                 dest_file_dir=optarg;
113                 break;
114             default:
115                 printf("%s -t modification_times -l modification_len -s souce_file_folder -d dest_file_folder",argv[0]);
116                 exit(0);
117         }
118     }
119     
120     if(access(source_file_dir,0)==-1)
121     {
122         printf("%s is not exist\n",source_file_dir);
123         return -1;
124     }
125     if(access(dest_file_dir,0)==-1)
126     {
127         if(mkdir(dest_file_dir,0775))
128         {
129             perror("mkdir dest folder failed \n");                   return -1;
130         }
131     }
132     listDir(source_file_dir,modification_times,modification_len,source_file_dir,dest_file_dir);
133     return 0;
134 }

Makefile

all:rand_file_modification

rand_file_modification : rand_file_modification.c
    gcc -g -Wall -o $@ $^

clean : 
    rm rand_file_modification 

 

posted on 2017-05-26 16:56  woshare  阅读(310)  评论(0编辑  收藏  举报