C++ 拷贝文件最简单快捷的两种方法

python拷贝文件十分的方便,C++就略显复杂了,查阅了多种方法,最终遴选出两种简单快捷的方法供大家参考。

方法1:基于ifstream及ofstream

#include <iostream>
#include <fstream>
#include <string>

void copyFile(std::string srcFilePath, std::string dstFilePath)
{
	std::ifstream  src(srcFilePath, std::ios::binary);
	std::ofstream  dst(dstFilePath, std::ios::binary);
	dst << src.rdbuf();
}

方法2:基于Windows API

#include <iostream>
#include <windows.h>  
#include <string>

// srcFilePath, dstFilePath为LPCWSTR
CopyFile(srcFilePath, dstFilePath, false);

通过测试,发现方法1比方法2更快速,但方法1会修改文件的“时间”属性,方法2不会。

猜你喜欢

转载自blog.csdn.net/chan1987818/article/details/129816881
今日推荐