#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. Jmeter+Badboy安装使用文档

                  Jmeter+Badboy安装使用文档       目录   1.jmeter安装    1 2.Jmeter基础使用    3 3. 使用Jmeter进行分布式测试    ...

  2. Laya 1.x 按文件夹TS代码合并

    Laya 1.x 使用TS开发时,经常会碰到代码文件太多,加载index.html时时间太长的问题.Laya编辑器貌似没有自带JS代码合并的功能.基于Laya去实现JS合并需要修改编辑器源码,合并JS ...

  3. Netty源码分析第2章(NioEventLoop)---->第4节: NioEventLoop线程的启动

    Netty源码分析第二章: NioEventLoop   第四节: NioEventLoop线程的启动 之前的小节我们学习了NioEventLoop的创建以及线程分配器的初始化, 那么NioEvent ...

  4. ABP.ModuleZero.Feature——特性管理

    原文地址:http://aspnetboilerplate.com/Pages/Documents/Feature-Management  (在翻译原文的基础上增补更多细节说明) 简介 在大多数SAA ...

  5. 打包应用和构建Docker镜像(docker在windows上)

    在构建Docker时编译应用 一般有两种方法在构建镜像时进行打包应用.第一种方法就是使用基本的镜像,该镜像包括应用平台和构建工具,因此在Dockerfile中,复制源代码到镜像中并在构建镜像时编译ap ...

  6. 学员管理系统(SQLAlchemy 实现)

    一.业务逻辑 二.设计表结构 三.代码结构 start.py import os, sys sys.path.insert(0, os.path.dirname(os.path.dirname(os. ...

  7. centos 切换用户显示bash-4.2$,不显示用户名路径的问题

    原文链接: http://blog.csdn.net/testcs_dn/article/details/70482468

  8. 事后分析报告(Postmortem Report)要求

    在得到M1 团队成绩之后, 每个团队都需要编写一个事后分析报告,对于团队在M1阶段的工作做一个总结. 请根据下面的模板总结并发表博客: http://www.cnblogs.com/xinz/arch ...

  9. Daily Scrum (2015/10/25)

    今天终于到了周末的尾声,我们的组员也应该正常得投入到工作中了.这天晚上我(符美潇)和PM(潘礼鹏)和两个DEV开了一个小会,讨论一下我们本周的代码编写工作.我们了解到大家的代码阅读工作和相关知识的学习 ...

  10. Scrum Meeting 11.03

    成员 今日任务 明日计划 用时 徐越 休息     赵庶宏 编写功能说明书,servlet代码移植 servlet代码移植 3h 薄霖 阅读上一届相关代码,思考改进方法 学习安卓界面设计数据库管理 4 ...