linuc 文件夹的拷贝
来源:互联网 发布:万网域名证书在线生成 编辑:程序博客网 时间:2024/05/18 11:30
转自:http://blog.csdn.net/yongh701/article/details/50403256
在《【Linux】利用C语言文件流复制单一文件》(点击打开链接)讲述了如何用C语言拷贝文件,但是这只能拷贝单一文件。如果你要用LinuxC拷贝整个文件夹,同样要像《【Java】利用文件输入输出流完成把一个文件夹内的所有文件拷贝的另一的文件夹的操作》(点击打开链接)一样,先用《【Linux】遍历某一目录,判断文件与文件夹,main参数》(点击打开链接)的方法遍历整个文件目录,之后再一个一个实现文件拷贝。具体代码如下:
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<dirent.h>//输出文件信息
- #include<sys/stat.h>//判断是否目录
- int is_dir(char* path){
- struct stat st;
- stat(path,&st);
- if(S_ISDIR(st.st_mode)){
- return 1;
- }
- else{
- return 0;
- }
- }
-
- int endwith(char* s,char c){
- if(s[strlen(s)-1]==c){
- return 1;
- }
- else{
- return 0;
- }
- }
- char* str_contact(char* str1,char* str2){
- char* result;
- result=(char*)malloc(strlen(str1)+strlen(str2)+1);
- if(!result){
- printf("字符串连接时,内存动态分配失败\n");
- exit(1);
- }
- strcat(result,str1);
- strcat(result,str2);
- return result;
- }
-
- void copy_file(char* source_path,char *destination_path){
- char buffer[1024];
- FILE *in,*out;
- if((in=fopen(source_path,"r"))==NULL){
- printf("源文件打开失败!\n");
- exit(1);
- }
- if((out=fopen(destination_path,"w"))==NULL){
- printf("目标文件创建失败!\n");
- exit(1);
- }
- int len;
- while((len=fread(buffer,1,1024,in))>0){
- fwrite(buffer,1,len,out);
- }
- fclose(out);
- fclose(in);
- }
- void copy_folder(char* source_path,char *destination_path){
- if(!opendir(destination_path)){
- if (mkdir(destination_path,0777))
- {
- printf("创建文件夹失败!");
- }
- }
- char *path;
- path=(char*)malloc(512);
- path=str_contact(path,source_path);
- struct dirent* filename;
- DIR* dp=opendir(path);
- while(filename=readdir(dp)){
- memset(path,0,sizeof(path));
- path=str_contact(path,source_path);
-
-
- char *file_source_path;
- file_source_path=(char*)malloc(512);
- if(!endwith(source_path,'/')){
- file_source_path=str_contact(file_source_path,source_path);
- file_source_path=str_contact(source_path,"/");
- }
- else{
- file_source_path=str_contact(file_source_path,source_path);
- }
- char *file_destination_path;
- file_destination_path=(char*)malloc(512);
- if(!endwith(destination_path,'/')){
- file_destination_path=str_contact(file_destination_path,destination_path);
- file_destination_path=str_contact(destination_path,"/");
- }
- else{
- file_destination_path=str_contact(file_destination_path,destination_path);
- }
-
- file_source_path=str_contact(file_source_path,filename->d_name);
- file_destination_path=str_contact(file_destination_path,filename->d_name);
- if(is_dir(file_source_path)){
- if(!endwith(file_source_path,'.')){
- copy_folder(file_source_path,file_destination_path);
- }
- }
- else{
- copy_file(file_source_path,file_destination_path);
- printf("复制%s到%s成功!\n",file_source_path,file_destination_path);
- }
- }
- }
-
- int main(int argc,char *argv[]){
- if(argv[1]==NULL||argv[1]==NULL){
- printf("请输入两个文件夹路径,第一个为源,第二个为目的!\n");
- exit(1);
- }
- char* source_path=argv[1];
- char* destination_path=argv[2];
- DIR* source=opendir(source_path);
- DIR* destination=opendir(destination_path);
- if(!source||!destination){
- printf("你输入的一个参数或者第二个参数不是文件夹!\n");
- }
- copy_folder(source_path,destination_path);
- return 0;
- }
运行结果如下,设置一个有文件夹,有文件的文件夹A,计划将里面的内容,拷贝到原本空空是也的文件夹B中。
上述程序中,难点有以下几个:
1、由于此程序涉及多次文件路径的拼接,因此最大的难题就是C语言中对于字符串的处理。众所周知,C语言中是没有字符串的概念,仅有字符数组,与指向这个字符数组的首位置的指针的概念。用char *path;配合path=(char*)malloc(512);相当于搞出一个,指向长达512的空字符数组的指针,此时path你可以理解为字符串。这样使用的strcat函数,不停地给path进行拼接,就不会出现段错误的内存溢出错误。
接下去用了大量的代码用于源与目标文件路径与文件名的拼接,其实完成的功能很简单,如果是其它语言,可能string file_source_path;string filename->d_name;之后就一句file_source_path+filename->d_name;
反正大家明白什么回事就行了,不必细究,我也不过是把《【Linux】纯C环境下字符串的处理》(点击打开链接)中方法搞过来。
2、还有一个问题,就是复制的时候,要注意排除当前文件夹中.与..这两个链接到上级目录与根目录的文件夹。在Linux似乎任何一个文件夹都有这个东东,一开始我写程序的时候,根本不知道,搞了一个复杂度超高还根本停不下来的递归迭代,这也是Linux与Windows不一样的地方。
3、其它地方也就没什么了,都是一些之前文件写过的东西。