copy(读取的路径或名字,目标文件的路径或名字)
{
if(读取的是一个文件夹)
{ 创造一个文件夹
打开文件夹,读取文件夹的内容
判断是否是一个文件夹 是:递归copy(xx,xx);
否,说明是个文件,拷贝文件
}
}
- int copy_r(char *read_dir_path, char *write_dir_path)
- {
- DIR * p_dir;
- struct dirent * p_dirent;
- char read_buf[256];
- char write_buf[256];
- sprintf(read_buf,"%s/", read_dir_path);
- sprintf(write_buf,"%s/",write_dir_path);
- strcat(read_dir_path,"/");
- if( Is_dir(read_buf) )
- {
- //mkdir
- if(mkdir(write_buf, 0755) < 0)
- {
- printf("mkdir fall:%s ", strerror(errno));
- return -1;
- }
- //opendir and readdir
- if((p_dir = opendir(read_buf)) == NULL )
- {
- printf("Usage:cp -r <src_dir> <dat_dir> error:%s ",strerror(errno));
- return -1;
- }
- while((p_dirent=readdir(p_dir)) != NULL)
- {
- char read_buffer[256];
- char write_buffer[256];
- sprintf(read_buffer,"%s%s", read_buf, p_dirent->d_name);
- sprintf(write_buffer,"%s%s", write_buf, p_dirent->d_name);
- printf("%s %s ",read_buffer,write_buffer);
- //it is a directory
- if( Is_dir(read_buffer) && 0 != strcmp(p_dirent->d_name, ".") && 0 != strcmp(p_dirent->d_name, "..") )
- {
- copy_r(read_buffer, write_buffer);
- }
- else if(0 != strcmp(p_dirent->d_name, ".") && 0 != strcmp(p_dirent->d_name, ".."))
- {
- //copy file
- if( file_copy_line(read_buffer, write_buffer) < 0 )
- return 0;
- }
- }
- closedir(p_dir);
- }
- }
原文地址http://www.linuxidc.com/Linux/2011-08/41472.htm
另一篇http://blog.csdn.net/blogofshuguang/article/details/17040915