没有任何可供使用的功能示例c++ without additional libs
将递归文件和文件夹复制到新位置。
一些替代方案system("cp -R -f dir");
call.
我只找到这个C 中的递归目录复制 https://stackoverflow.com/questions/15177378/recursive-directory-copying-in-c线程答案上的示例,但它还没有准备好使用,我不确定这个示例是否正确开始。
也许有人在磁盘上有工作示例?
这是使用 POSIX 和标准库函数进行递归复制的完整运行示例。
#include <string>
#include <fstream>
#include <ftw.h>
#include <sys/stat.h>
const char* src_root ="foo/";
std::string dst_root ="foo2/";
constexpr int ftw_max_fd = 20; // I don't know appropriate value for this
extern "C" int copy_file(const char*, const struct stat, int);
int copy_file(const char* src_path, const struct stat* sb, int typeflag) {
std::string dst_path = dst_root + src_path;
switch(typeflag) {
case FTW_D:
mkdir(dst_path.c_str(), sb->st_mode);
break;
case FTW_F:
std::ifstream src(src_path, std::ios::binary);
std::ofstream dst(dst_path, std::ios::binary);
dst << src.rdbuf();
}
return 0;
}
int main() {
ftw(src_root, copy_file, ftw_max_fd);
}
请注意,使用标准库进行的简单文件复制不会复制源文件的模式。它还深度复制链接。也可能会忽略一些我没有提到的细节。如果您需要以不同方式处理这些问题,请使用 POSIX 特定函数。
我建议使用 Boost,因为它可以移植到非 POSIX 系统,并且新的 C++ 标准文件系统 API 将基于它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)