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

Makefile

  1. all:rand_file_modification
  2.  
  3. rand_file_modification : rand_file_modification.c
  4. gcc -g -Wall -o $@ $^
  5.  
  6. clean :
  7. rm rand_file_modification

linux c 判断文件存在,遍历文件,随机修改文件内容的更多相关文章

  1. 再说linux中的rm mv 遍历执行多个文件的操作: find + xagrs

    参考文章: http://cfqtyaogang.blog.163.com/blog/static/218051022011812111342203/, 这篇文章讲得很全面很详细... 包括不好理解的 ...

  2. Python文件操作大全,随机删除文件夹内的任意文件

     在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法: os.path.abspath(path) #返回绝对路径os.path.basename(path ...

  3. linux仅修改文件夹权限 分别批量修改文件和文件夹权限

    比如我想把/var/www/html下的文件全部改成664,文件夹改成775,怎么做呢 方法一: 先把所有文件及文件夹改成664,然后把所有文件夹改成775 chmod -R 664 ./ find ...

  4. 【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改

    创建文件和读取文件的值 #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体 public FormMain() { InitializeComponent(); //ReadFile ...

  5. Linux shell 脚本攻略之创建不可修改文件

    摘自:<Linux shell 脚本攻略>

  6. 转载:Linux目录文件的权限查看与修改

    ########################################                                                             ...

  7. Java I/O---RandomAccessFile类(随机访问文件的读取和写入)

    1.JDK API中RandomAccessFile类的描述 此类的实例支持对随机访问文件的读取和写入.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组.存在指向该隐含数组的光标或索引 ...

  8. linux修改文件所有者和文件所在组

      chgrp  用户名    文件名  -R chown 用户名   文件名  -R -R表示递归目录下所有文件 以上部分已验证 一.修改文件所属组群——chgrp    修改文件所属组群很简单-c ...

  9. 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录

    在Linux中,任何设备都是文件,不仅如此,连数据通信的接口也有专门的文件负责.可以说,一切皆文件,目录也是一种文件,是路径映射.因此,文件系统是Linux的基础. 一.文件与目录管理命令 1.ls( ...

随机推荐

  1. Openstack入门篇(十八)之Cinder服务-->使用NFS作为后端存储

    1.安装cinder-volume组件以及nfs [root@linux-node2 ~]# yum install -y openstack-cinder python-keystone [root ...

  2. springmvc ajax传递json值时出现400错误 (问题出在sql上)

    问题出在sql的int不能达到 10000000000 这个值,从 int(11) 修改成 varchar(20) 就可以正常存储了

  3. [BZOJ4484][JSOI2015]最小表示[拓扑排序+bitset]

    题意 给你一个 \(n\) 个点 \(m\) 条边的 \(\rm DAG\) ,询问最多能够删除多少条边,使得图的连通性不变 \(n\leq 3\times 10^4\ ,m\leq 10^5\) . ...

  4. SQL Server 的通用分页显示存储过程(转载)

    http://database.51cto.com/art/200512/12923.htm 建立一个 Web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是: ...

  5. Python调用matplotlib实现交互式数据可视化图表案例

    交互式的数据可视化图表是 New IT 新技术的一个应用方向,在过去,用户要在网页上查看数据,基本的实现方式就是在页面上显示一个表格出来,的而且确,用表格的方式来展示数据,显示的数据量会比较大,但是, ...

  6. 六种流行的语言大餐---C、C++、python、Java、php、C#你更喜欢哪一个呢?

    引言 鉴于五一期间超大的人流量,LZ思来想去,最终还是选择蜗居在自己的出租屋.无聊之际,当然不能忘了做点什么事情,于是LZ就研究了一下几种语言的皮毛,在这里献丑一翻,希望各位猿友莫要见笑. 不过说来也 ...

  7. MySQL与DevC++的连接问题

    0.引言 MySQL作为当前的一个主流的开源的关系型数据库,受到大家的广泛关注.DevC++也作为一个开源的简单的C++编辑器,有着比VS更简便更快捷的编译.但是目前网上,却很少有对 MySQL连接D ...

  8. 沧桑巨变中焕发青春活力-记极1s HC5661A 打怪升级之路

    最近发现一个新货umaxhosting年付10美元的便宜VPS.2杯喜茶的价格可以让你在国外拥有一个1024MB (1GB) DDR3 RAM.1024MB (1GB) vSwap.70GB RAID ...

  9. 通过Task异步加快对数组的运算

    一.介绍 Task是.NetFramework3.0出现的,线程是基于线程池,然后提供了丰富的API. 先用AverageAssign方法把一组数据平均分成n组,再通过遍历n组数据,循环开Task多线 ...

  10. Linux系统中时间区域和API

    1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...