Linux文件编程有两种方法:
一是Linux系统调用
二是C的库函数
其中前者依赖于Linux系统,后者与操作系统是独立的,在任何操作系统中,使用C的库函数操作文件的方法都是相同的。所以,有C语言基础很容易编写此程序。
/*Linux系统调用*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
char buff[1024];
int len;
char const *src = argv[1];//源路径
char const *des = argv[2];//目的路径
int f1,f2;//文件指示符
fd = open(src_path,O_RDWR|O_CREAT);//打开源文件,具有可读可写权限
fd2 = open(des_path,O_RDWR|O_CREAT);
while(len = read(fd,buff,1024))//用while循环依次写入目的文件
{
write(fd2,buff,len);
}
return 0;
}
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
char buff[1024];
int len;
int main(int argc, char const *argv[])
{
FILE *in,*out;
char const * src_path = argv[1]; //要被拷贝的文件路径
char const * des_path = argv[2]; //拷贝的文件放在哪里(路径)
in = fopen(argv[1],"r+");
out = fopen(argv[2],"w+");
while(len = fread(buff,1,sizeof(buff),in))
{
fwrite(buff,1,len,out);
}
return 0;
}
参考博客:https://blog.youkuaiyun.com/u014453898/article/details/54864666