用C语言编写一个程序,运行程序时,附上参数(想要复制的文件夹,以及目标文件夹)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int is_dir( char* file_name);
void cp_file( char *source_path , char *destination_path)
{
FILE* fp_src = NULL;
FILE* fp_dst = NULL;
// int in,out;
if((fp_src = fopen(source_path,"r"))==NULL){//打开源文件的文件流
printf("源文件打开失败!\n");
exit(1);
}
if((fp_dst=fopen(destination_path,"w"))==NULL){//打开目标文件的文件流
printf("目标文件创建失败!\n");
exit(1);
}
int c, total = 0;
while(1)
{
c = fgetc( fp_src);
if( c == EOF && feof(fp_src))
{
break;
}
else if( ferror(fp_src))
{
perror("fget()");
break;
}
fputc(c,fp_dst);
}
fclose(fp_src);
fclose(fp_dst);
}
int endwith(char* s,char c){//用于判断字符串结尾是否为“.”
printf("end : %c\n",s[strlen(s)-1]);
if(s[strlen(s)-1]==c){
return 1;
}
else{
return 0;
}
}
void copy_folder(char* source_path,char *destination_path)
{
DIR *dst_dp = opendir(destination_path);
if(dst_dp == NULL)// if destination_path is not exist , mkdir new one
{
printf(" your dest dir is not existed \n");
printf(" system will mkdir for U \n");
if(mkdir(destination_path,0777) == -1)
{
printf(" error occur during mkdir");
exit(-1);
}
}
printf(" your dir is correct !\n");// remind user input dir is correct
DIR *src_dp = opendir(source_path);
struct dirent *ep_src = readdir(src_dp);
char address[108] = {0};
char toaddress[108] = {0};
while(1)//this is a recursion,break until ( all files in source_path have been copied , or error occured)
{
sprintf(address,"%s%s",source_path,"/");//ep_src->d_name can't be passed to function as argu
sprintf(address,"%s%s",address,ep_src->d_name);//so we should use sprintf() to catch address
printf(" source_path : %s\n",address);
sprintf(toaddress,"%s%s",destination_path,"/");
sprintf(toaddress,"%s%s",toaddress,ep_src->d_name);
printf(" dest_path : %s\n",toaddress);
printf(" got %d\n",is_dir(address));
printf(" %d\n",endwith(address,'.'));
if(endwith(address,'.') == 1)//if the file is . or .. pass
{
printf(" program src jump the %s\n",address);
// ep_src = readdir(src_dp);
}
else if( ( is_dir(address) != 1) )//if the file is not fir just copy file
{
printf(" in else if");
cp_file(address,toaddress);
// ep_src = readdir(src_dp);
}
else
{
printf(" i am copying your folder\n %s to %s\n",address,toaddress);
copy_folder(address,toaddress);// when test the file is a dir , call copy_folder function again
}
if((ep_src = readdir(src_dp)) == NULL )// if all files in address have been copied , break;
break;
memset(address,108,1);
memset(toaddress,108,1);
}
closedir(dst_dp);
closedir(src_dp);
}
int is_dir( char* file_name)
{
struct stat info;
stat(file_name,&info);
if(S_ISDIR(info.st_mode))
return 1;
else
return 0;
}
int main( int argc , char** argv)
{
if(argc != 3)
{
printf("Usage: %s \n",argv[0]);
exit(1);
}
printf(" main function\n");
copy_folder(argv[1],argv[2]);
return 0;
}
这个程序需要使用到递归的思想,什么时候结束呢,所有的文件被复制完,这里要注意,linux系统中有隐藏的.,…文件,不能复制,需要跳过,
这里怎么知道复制完了呢,使用readdir(),
The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occurred.
每一次读取后后目录项指针偏移到下一项,当到目录末尾 目录项指针指向空,其他的就是复制当前目录下其他文件(包括普通文件和目录文件)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int is_dir( char* file_name);
void cp_file( char *source_path , char *destination_path)
{
FILE* fp_src = NULL;
FILE* fp_dst = NULL;
// int in,out;
if((fp_src = fopen(source_path,"r"))==NULL){//打开源文件的文件流
printf("源文件打开失败!\n");
exit(1);
}
if((fp_dst=fopen(destination_path,"w"))==NULL){//打开目标文件的文件流
printf("目标文件创建失败!\n");
exit(1);
}
int c, total = 0;
while(1)
{
c = fgetc( fp_src);
if( c == EOF && feof(fp_src))
{
break;
}
else if( ferror(fp_src))
{
perror("fget()");
break;
}
fputc(c,fp_dst);
}
fclose(fp_src);
fclose(fp_dst);
}
int endwith(char* s,char c){//用于判断字符串结尾是否为“.”
printf("end : %c\n",s[strlen(s)-1]);
if(s[strlen(s)-1]==c){
return 1;
}
else{
return 0;
}
}
void copy_folder(char* source_path,char *destination_path)
{
if(opendir(destination_path) == NULL)// if destination_path is not exist , mkdir new one
{
printf(" your dest dir is not existed \n");
printf(" system will mkdir for U \n");
if(mkdir(destination_path,0777) == -1)
{
printf(" error occur during mkdir");
exit(-1);
}
}
printf(" your dir is correct !\n");// remind user input dir is correct
DIR *dst_dp = opendir(destination_path);
DIR *src_dp = opendir(source_path);
struct dirent *ep_src = readdir(src_dp);
char address[108] = {0};
char toaddress[108] = {0};
while(1)//this is a recursion,break until ( all files in source_path have been copied , or error occured)
{
sprintf(address,"%s%s",source_path,"/");//ep_src->d_name can't be passed to function as argu
sprintf(address,"%s%s",address,ep_src->d_name);//so we should use sprintf() to catch address
printf(" source_path : %s\n",address);
sprintf(toaddress,"%s%s",destination_path,"/");
sprintf(toaddress,"%s%s",toaddress,ep_src->d_name);
printf(" dest_path : %s\n",toaddress);
printf(" got %d\n",is_dir(address));
printf(" %d\n",endwith(address,'.'));
if(endwith(address,'.') == 1)//if the file is . or .. pass
{
printf(" program src jump the %s\n",address);
// ep_src = readdir(src_dp);
}
else if( ( is_dir(address) != 1) )//if the file is not fir just copy file
{
printf(" in else if");
cp_file(address,toaddress);
// ep_src = readdir(src_dp);
}
else
{
printf(" i am copying your folder\n %s to %s\n",address,toaddress);
copy_folder(address,toaddress);// when test the file is a dir , call copy_folder function again
}
if((ep_src = readdir(src_dp)) == NULL )// if all files in address have been copied , break;
break;
memset(address,108,1);
memset(toaddress,108,1);
}
}
int is_dir( char* file_name)
{
struct stat info;
stat(file_name,&info);
if(S_ISDIR(info.st_mode))
return 1;
else
return 0;
}
int main( int argc , char** argv)
{
if(argc != 3)
{
printf("Usage: %s \n",argv[0]);
exit(1);
}
printf(" main function\n");
copy_folder(argv[1],argv[2]);
return 0;
}