我用谷歌搜索了这个,但我仍然对如何使用它感到困惑.我正在制作文件管理器,我希望能够将文件复制并粘贴到新目录中.我知道要复制我需要使用file.copy(),但我不知道如何在我的代码中实现它.
我想用fstream做这个.
如果您使用的是Win32 API,请考虑查看函数CopyFile或CopyFileEx.
您可以使用类似于以下内容的方式使用第一个:
CopyFile( szFilePath.c_str(), szCopyPath.c_str(), FALSE );
Run Code Online (Sandbox Code Playgroud)
这会将在内容中找到的文件复制szFilePath到内容中szCopyPath,FALSE如果复制不成功则返回.要了解有关函数失败原因的更多信息,可以使用该GetLastError()函数,然后在Microsoft文档中查找错误代码.
void copyFile(const std::string &from, const std::string &to)
{
std::ifstream is(from, ios::in | ios::binary);
std::ofstream os(to, ios::out | ios::binary);
std::copy(std::istream_iterator(is), std::istream_iterator(),
std::ostream_iterator(os));
}
Run Code Online (Sandbox Code Playgroud)