#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<assert.h>
#include<string.h>
#include<dirent.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h> #define PATH_LEN 512
//#define MODIFICATION_LEN 10
//#define MODIFICATION_TIMES 5
char source_file_folder[]="t5";
char dest_file_folder[]="dest_file"; int modification(char src_file[],int MODIFICATION_TIMES,int MODIFICATION_LEN,char source_file_dir[],char dest_file_dir[])
{
FILE *src_fp,*dst_fp;
char src_file_path[PATH_LEN];
char dst_file_path[PATH_LEN];
snprintf(src_file_path,PATH_LEN,"%s/%s",source_file_dir,src_file);
snprintf(dst_file_path,PATH_LEN,"%s/%s",dest_file_dir,src_file);
src_fp=fopen(src_file_path,"a+");
assert(src_fp!=NULL);
fseek(src_fp,,SEEK_END);
int src_file_len=ftell(src_fp);
assert(src_file_len!=-);
rewind(src_fp);
int *random_offset=(int *)calloc(MODIFICATION_TIMES,sizeof(int));
assert(random_offset!=NULL);
srand((unsigned)time(NULL));
int i=;
do
{
random_offset[i]=rand()%src_file_len;
}while(random_offset[i++]<=src_file_len && i<MODIFICATION_TIMES);
dst_fp=fopen(dst_file_path,"w+");
assert(dst_fp!=NULL);
char *buff=(char *)malloc(src_file_len*sizeof(char));
assert(buff!=NULL);
if(fread(buff,src_file_len,,src_fp)==)
fwrite(buff,src_file_len,,dst_fp);
//printf("src file : %s\n",buff);
//free(buff);
i=;
char *center_buff;
while(i<MODIFICATION_TIMES)
{
int right_offset=random_offset[i]+MODIFICATION_LEN;
right_offset=right_offset>src_file_len?src_file_len:right_offset;
int center_len=right_offset-random_offset[i];
center_buff=(char *)calloc(center_len,sizeof(char));
memset(center_buff,'',center_len);
fseek(dst_fp,random_offset[i],SEEK_SET);
fwrite(center_buff,center_len,,dst_fp);
free(center_buff);
++i;
}
free(buff);
fclose(src_fp);
fclose(dst_fp);
return ; } void listDir(char *path,int modification_times,int modification_len,char source_file_dir[],char dest_file_dir[])
{
DIR *pDir;
struct dirent *ent;
char childpath[PATH_LEN];
pDir=opendir(path);
memset(childpath,,sizeof(childpath));
while((ent=readdir(pDir))!=NULL)
{
if(strcmp(ent->d_name,".")==||strcmp(ent->d_name,"..")==)
continue;
// if(ent->d_type==8)
snprintf(childpath,PATH_LEN,"%s/%s",path,ent->d_name);
printf("%s\n",childpath);
modification(ent->d_name,modification_times,modification_len,source_file_dir,dest_file_dir);
}
closedir(pDir);
}
int main(int argc,char *argv[])
{
char ch;
char *source_file_dir=source_file_folder;
char *dest_file_dir=dest_file_folder;
int modification_len=,modification_times=;
if(argc<)
{
printf("Usage:%s -t modification_times -l modification_len -s source_file_folder -d dest_file_folder\n",argv[]);
return ;
}
while((ch=getopt(argc,argv,"t:l:s:d:"))!=-)
{
switch(ch)
{
case 't':
modification_times=atoi(optarg);
break;
case 'l':
modification_len=atoi(optarg);
break;
case 's':
source_file_dir=optarg;
break;
case 'd':
dest_file_dir=optarg;
break;
default:
printf("%s -t modification_times -l modification_len -s souce_file_folder -d dest_file_folder",argv[]);
exit();
}
} if(access(source_file_dir,)==-)
{
printf("%s is not exist\n",source_file_dir);
return -;
}
if(access(dest_file_dir,)==-)
{
if(mkdir(dest_file_dir,))
{
perror("mkdir dest folder failed \n"); return -;
}
}
listDir(source_file_dir,modification_times,modification_len,source_file_dir,dest_file_dir);
return ;
}

Makefile

all:rand_file_modification

rand_file_modification : rand_file_modification.c
gcc -g -Wall -o $@ $^ clean :
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. 洛咕 P3964 [TJOI2013]松鼠聚会

    有个结论就是把坐标\((x,y)\)变形成\(((x+y)/2,(x-y)/2)\),切比雪夫距离就变成了曼哈顿距离. 所以变换一下坐标直接统计答案即可. // luogu-judger-enable ...

  2. CSS快速入门-箭头和图标

    一.三步搞懂箭头产生的原理 在前面的盒子模型一文中,我们已经知道了一个元素空间占位.为了弄明白箭头的产生,我们可以三步走: #demo12 { border: 100px solid; border- ...

  3. Python distribute到底使用package_data还是MANIFEST.in?

    今天看Flask的文档,里面提到如何通过distribute发布你自己的Python包.讲包含文件的时候,提到要用MANIFEST.in并将include_package_data设置为True. 由 ...

  4. sqlite3 数据库命令操作

    SQLite 数据库,是一个非常轻量级自包含(lightweight and self-contained)的DBMS,它可移植性好,很容易使用,很小,高效而且可靠. SQLite嵌入到使用它的应用程 ...

  5. python002

    1.万恶的”+“号字符串拼接 字符串中的连接符+”会开辟一个新的空间,多一个“+“就会多开辟一个空间,影响性能 2.字符串格式化 ”%S“   :字符类型         ”%D“   ”数字类型  ...

  6. 接口测试中抓包工具Charles的使用

    在被测接口并没有明确的接口文档给出时,我们需要借助抓包工具来帮助测试,利用抓包工具我们几乎可以获得接口文档中能给你的一切.常见的抓包工具有Charles和Fiddler, Fiddler只能用在Win ...

  7. 'javac' 不是内部或外部命令,也不是可运行的程序 或批处理文件.

    如果你只需要使用javac命令不需要如此复杂的!! 你先把你自己新建的JAVA_HOME.CLASSPATH这两个变量和PATH变量中的JAVA_HOME%\bin;%JAVA_HOME%\jre\b ...

  8. svn图文教程-宋正河整理

    下载地址:http://download.csdn.net/download/songzhengdong82/4433476 在线浏览:http://wenku.baidu.com/view/07f1 ...

  9. 【Skynet】Traceback汇总

    error: ./skynet/lualib/skynet.lua:534: ./skynet/lualib/skynet.lua:156: ./logic/gate/socket_msg.lua:5 ...

  10. Dubbo问题处理集合

    1 . 启动微服务的时候,报错信息如下: 核心:Can not lock the registry cache file /root/.dubbo/dubbo-registry-127.0.0.1.c ...