C++ 使用 STL 库判断是文件夹还是文件并实现文件复制
#include#include #include //判断文件是否是一个有效的文件 bool is_valid_file(const char* szFile) { if (0 == _access(szFile, 00)) { struct _stat buf_stat; int result = _stat(szFile, &buf_stat); if (_S_IFREG & buf_stat.st_mode) { printf("filen"); return true; } else if (_S_IFDIR & buf_stat.st_mode) { printf("foldern"); } } return false; } bool copy_file(const char* szSrcFile, const char* szDstFile, bool bFailIfExists) { if (bFailIfExists && is_valid_file(szDstFile)) { return false; } std::ifstream in; in.open(szSrcFile, std::ios_base::binary); if (!in) { std::cout << "open src file : " << szSrcFile << " failed" << std::endl; return false; } std::ofstream out; out.open(szDstFile, std::ios_base::binary | std::ios_base::trunc); if (!out) { std::cout << "create new file : " << szDstFile << " failed" << std::endl; in.close(); return false; } out << in.rdbuf(); out.close(); in.close(); return true; } int main(int argc, char *argv[]) { copy_file("E:\test.dat", "E:\test2.dat", true); std::cout << "hello world." << std::endl; return 0; }
参考:
https://blog.csdn.net/u012750702/article/details/52738859



