linux c 判断文件存在,遍历文件,随机修改文件内容
#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 判断文件存在,遍历文件,随机修改文件内容的更多相关文章
- 再说linux中的rm mv 遍历执行多个文件的操作: find + xagrs
参考文章: http://cfqtyaogang.blog.163.com/blog/static/218051022011812111342203/, 这篇文章讲得很全面很详细... 包括不好理解的 ...
- Python文件操作大全,随机删除文件夹内的任意文件
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法: os.path.abspath(path) #返回绝对路径os.path.basename(path ...
- linux仅修改文件夹权限 分别批量修改文件和文件夹权限
比如我想把/var/www/html下的文件全部改成664,文件夹改成775,怎么做呢 方法一: 先把所有文件及文件夹改成664,然后把所有文件夹改成775 chmod -R 664 ./ find ...
- 【转】c# winform 创建文件,把值写入文件,读取文件里的值,修改文件的值,对文件的创建,写入,修改
创建文件和读取文件的值 #region 判断文件是否存在,不存在则创建,否则读取值显示到窗体 public FormMain() { InitializeComponent(); //ReadFile ...
- Linux shell 脚本攻略之创建不可修改文件
摘自:<Linux shell 脚本攻略>
- 转载:Linux目录文件的权限查看与修改
######################################## ...
- Java I/O---RandomAccessFile类(随机访问文件的读取和写入)
1.JDK API中RandomAccessFile类的描述 此类的实例支持对随机访问文件的读取和写入.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组.存在指向该隐含数组的光标或索引 ...
- linux修改文件所有者和文件所在组
chgrp 用户名 文件名 -R chown 用户名 文件名 -R -R表示递归目录下所有文件 以上部分已验证 一.修改文件所属组群——chgrp 修改文件所属组群很简单-c ...
- 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录
在Linux中,任何设备都是文件,不仅如此,连数据通信的接口也有专门的文件负责.可以说,一切皆文件,目录也是一种文件,是路径映射.因此,文件系统是Linux的基础. 一.文件与目录管理命令 1.ls( ...
随机推荐
- 1997: [Hnoi2010]Planar
1997: [Hnoi2010]Planar 链接 分析: 首先在给定的那个环上考虑进行操作,如果环内有有两条边相交,那么可以把其中的一条放到环的外面去.所以转换为2-sat问题. 像这样,由于1-4 ...
- Intellij IDEA《十分钟,配置struts2》by me
1.加载Struts 2类库 <dependencies> <!-- Struts 2 核心包--> <dependency> <groupId>org ...
- Bailian 2808 校门外的树(入门线段树)
题目链接:http://bailian.openjudge.cn/practice/2808?lang=en_US 总时间限制: 1000ms 内存限制: 65536kB 描述 某校大门外长度为L的马 ...
- csb反编译为csd,并自动进行资源的删除
好多人都想将csb进行反编译为csd,然后进行资源的清理 目前自己的项目也遇到了类似的问题,所以进行了整理 还有很多不完善的地方,后续会一步步加深 请大家多多指教 下载链接:https://pan.b ...
- Unity扩展编辑器一
将Test1脚本挂在摄像机上 如图展示 下面我们需要在代码中动态的编辑它,请在你的Project视图中创建一个Editor文件夹,把MyEditor放进Editor文件夹中 在OnInsp ...
- 《杜增强讲Unity之Tanks坦克大战》3-添加坦克
3 添加坦克 3.1 本节效果预览 3.2 另存新场景 首先打开上次的场景s1,另存为s2,放到同一个文件夹下面. 3.3 添加坦克模型 在Model文件夹下面找到Tank模型 将Tank ...
- FFM原理及公式推导
原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun 上一篇讲了FM(Factorization Machines),说一说FFM ...
- 在Gulp中使用BrowserSync
博客已迁移至http://zlwis.me. 很早就听说过BrowserSync,也看过一些相关文章,可就是没用过.之前一直在用Gulp开发项目,每次编写完Sass后还要用按F5刷新页面看效果,想想也 ...
- webpack入门指南-step02
webpack 安装 1)安装前的准备:webpack是基于node环境的项目,所以使用前必须先安装node和npm. 在安装 Webpack 前,你本地环境需要支持 node.js.如果电脑没有装过 ...
- 20135316王剑桥 linux第六周课实验笔记
6.存储器层次结构 6.1存储技术 1.如果你的程序需要的数据是存储在CPU寄存器中的,那么在执行期间,在零个周期内就能访问到它们.如果存储在高速缓冲中,需要1-10个周期.如果存储在主存中,需要50 ...