#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. set方法在set传入值时报空指针异常,直接设置定值即可

    这种情况可能跟上下的程序有关,所以直接设置定值传入即可. 例如: re.setRid(ar.getRid()); // 这个是报错代码 md.setConnMailStatusTrue(ar.getR ...

  2. 动态加载与插件系统的初步实现(四):解析JSON、扩展Fiddler

    按文章结构,这部分应该给出WCFRest项目示例,我想WinForm示例足够详尽了,况且WCFRest还不需要使用插件AppDomain那一套,于是把最近写的Fiddler扩展搬上来吧. Fiddle ...

  3. 关于dbw 与dbm 的计算

    一分贝(dB)表示单元信号强度的相对差异.其比率的基础对数为10,,如dB = 10 x Log10 (P1/P2). 基础10对数规则: Log10 (AxB) = Log10(A) + Log10 ...

  4. Fiddler 抓包https配置 提示:creation of the root certificate was not successful 证书安装不成功

    window7 提示:creation of the root certificate was not successful 证书安装不成功 1.cmd 命令行   找到fiddler的安装目录  如 ...

  5. WebGL——水波纹特效

    大家好,今天我ccentry要做一个水波纹特效,我们来看看水波纹特效的做法.首先我们来看一下水波纹特效的效果是怎么样的,请看下图. 我们要做的就是类似这种纹理特效,那么我们来看看是如何制作的吧.首先鲫 ...

  6. 关于jsp中引用css外部样式无效时的处理方法

    今天做项目遇到的一个小问题,如下所示: <link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet ...

  7. 【Kubernetes】基于角色的权限控制:RBAC

    Kubernetes中所有的API对象,都保存在Etcd里,对这些API对象的操作,一定都是通过访问kube-apiserver实现的,原因是需要APIServer来做授权工作. 在Kubernete ...

  8. noip2018 D1T3 赛道修建

    题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,…,n1,2,…,n,有 n-1n−1 条适合于修建赛道的双向通 ...

  9. Vue 入门之组件化开发

    Vue 入门之组件化开发 组件其实就是一个拥有样式.动画.js 逻辑.HTML 结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue 的组件和也做的非常 ...

  10. Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValuesByConditionsNoPage

    报Invalid bound statement (not found): com.example.managerdemo.mapper.SingleTableMapper.selectAllValu ...