linux c实现目录复制,linux c 实现目录拷贝

linux c 实现目录拷贝

2018-11-19

[cpp]view plaincopy#include #include #include #include #include #include #include #ifndef DEBUG#define PDEBUG(fmt, args...)/do{}while(0)#else#define PDEBUG(fmt, args...)/printf("[%s:%d]"fmt, __func__, __LINE__, ##args)#endif#ifndef ERROR#define PERROR(err)/do{}while(0)#else#define PERROR(err)/printf("[%s:%d]/n", __func__, __LINE__);/perror(err)#endif#define BUFFER_SIZE 1024/** 判断是否是目录* @ 是目录返回1,是普通文件返回0,出错返回-1* */int IsDir(const char *path){struct stat buf;if (stat(path, &buf)==-1){PERROR("stat");PDEBUG("path = %s/n", path);return -1;}return S_ISDIR(buf.st_mode);}/** 创建目录* @ 可以创建多级目录,失败返回-1* */int CreateDir(const char *path){char pathname[256];strcpy(pathname, path);int i, len=strlen(pathname);if (pathname[len-1]!="/"){strcat(pathname, "/");len++;}for (i=0; id_name);printf("%s/n", frompath);strcat(topath, ptr->d_name);if ((ret=IsDir(frompath))==1){if (strcmp(strrchr(frompath, "/"), "/.")==0|| strcmp(strrchr(frompath, "/"), "/..")==0){PDEBUG(". or ..目录不用复制/n");}else{DirCopy(frompath, topath);}}else if (ret!=-1){FileCopy(frompath, topath);}}closedir(dir);return 0;}int main(int argc, char *argv[]){int ret;if ((ret=IsDir(argv[1]))==1){PDEBUG("Is a dir/n");}else if (ret!=-1){PDEBUG("Is a file/n");}DirCopy(argv[1], argv[2]);return 0;}

你的赞赏是我坚持原创的动力!

赞赏

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。

http://www.pinlue.com/style/images/nopic.gif