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( ...
随机推荐
- 异常 java.lang.NullPointerException at org.apache.jsp.index_jsp._jspService(index_jsp.java:124)
这是jsp报的异常 jsp代码: <% String LoginUsername = ""; String LoginPassword = ""; try ...
- 【ASP.NET Core】运行原理(1):创建WebHost
本系列将分析ASP.NET Core运行原理 [ASP.NET Core]运行原理[1]:创建WebHost [ASP.NET Core]运行原理[2]:启动WebHost [ASP.NET Core ...
- Idea for Mac 快捷键(快捷键选择:Mac OS X 10.5+)
删除一行 command + delete 查找 command + f 查找替换 command + r 复制一行 comma ...
- Jmeter+ant+jenkins接口自动化测试 平台搭建(二)
一.依赖文件配置 1.在ant目录C:\apache-ant-1.10.5下新建一个demo文件夹,并将jmeter测试脚本放在该文件夹中 2.将\apache-jmeter-3.3\extras下面 ...
- RobotFramework测试环境搭建记录
Robotframwork测试环境搭建记录 1.安装Python2.7(https://www.python.org/) 在环境变量path中加入“C:\Python27” 安装后的验证方法为在命令行 ...
- 2.5 Oracle之存储过程和MERGE INTO语句
一.MERGE INTO语句 1.merge into语句的功能:我们操作数据库的时候,有时候会遇到insert或者Update这种需求.我们操纵代码时至少需要写一个插入语句和更新语句并且还得单独写方 ...
- python装饰器(披着羊皮的狼)
python装饰器的作用是在不改变原有函数的基础上,对函数的功能进行增加或者修改. 装饰器语法是python语言更加优美且避免很多繁琐的事情,flask中配置路由的方式便是装饰器. 首先python中 ...
- 【Ansible】ansible循环
Ansible 循环 一.简单介绍 在ansible2.5之前,大多数人使”with_XXX”类型的关键字来操作循环,但是从2.6版本开始,官方推荐是”loop”关键字代替” with_XXX”. 1 ...
- nodejs 中jead模板改为ejs
var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set(' ...
- linux递归查找文件内容并替换
sed -i 's/原字符串/替换后字符串/g' `grep '搜索关键字' -rl /data/目标目录/ --include "*.html"` 上面是递归查找目录中所有的HT ...