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( ...
随机推荐
- Openstack入门篇(十八)之Cinder服务-->使用NFS作为后端存储
1.安装cinder-volume组件以及nfs [root@linux-node2 ~]# yum install -y openstack-cinder python-keystone [root ...
- springmvc ajax传递json值时出现400错误 (问题出在sql上)
问题出在sql的int不能达到 10000000000 这个值,从 int(11) 修改成 varchar(20) 就可以正常存储了
- [BZOJ4484][JSOI2015]最小表示[拓扑排序+bitset]
题意 给你一个 \(n\) 个点 \(m\) 条边的 \(\rm DAG\) ,询问最多能够删除多少条边,使得图的连通性不变 \(n\leq 3\times 10^4\ ,m\leq 10^5\) . ...
- SQL Server 的通用分页显示存储过程(转载)
http://database.51cto.com/art/200512/12923.htm 建立一个 Web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是: ...
- Python调用matplotlib实现交互式数据可视化图表案例
交互式的数据可视化图表是 New IT 新技术的一个应用方向,在过去,用户要在网页上查看数据,基本的实现方式就是在页面上显示一个表格出来,的而且确,用表格的方式来展示数据,显示的数据量会比较大,但是, ...
- 六种流行的语言大餐---C、C++、python、Java、php、C#你更喜欢哪一个呢?
引言 鉴于五一期间超大的人流量,LZ思来想去,最终还是选择蜗居在自己的出租屋.无聊之际,当然不能忘了做点什么事情,于是LZ就研究了一下几种语言的皮毛,在这里献丑一翻,希望各位猿友莫要见笑. 不过说来也 ...
- MySQL与DevC++的连接问题
0.引言 MySQL作为当前的一个主流的开源的关系型数据库,受到大家的广泛关注.DevC++也作为一个开源的简单的C++编辑器,有着比VS更简便更快捷的编译.但是目前网上,却很少有对 MySQL连接D ...
- 沧桑巨变中焕发青春活力-记极1s HC5661A 打怪升级之路
最近发现一个新货umaxhosting年付10美元的便宜VPS.2杯喜茶的价格可以让你在国外拥有一个1024MB (1GB) DDR3 RAM.1024MB (1GB) vSwap.70GB RAID ...
- 通过Task异步加快对数组的运算
一.介绍 Task是.NetFramework3.0出现的,线程是基于线程池,然后提供了丰富的API. 先用AverageAssign方法把一组数据平均分成n组,再通过遍历n组数据,循环开Task多线 ...
- Linux系统中时间区域和API
1.问题 在开发云平台程序的时候,经常会碰到时间区域转换的问题.比如,任何网络存储的文档的metadata都自己记录了编辑时间.但是,云平台记录时需要把这个时间转成标准时间,便于管理.但是用户使用的时 ...