如何使用 C++ 在 Windows 中复制和粘贴文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17929713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I copy and paste a file in Windows using C++?
提问by
I have googled this, but I am still confused about how to use it. I am making a file manager, and I want to be able t o copy and paste a file into a new directory. I know to copy I need to use file.copy()
, but I am not sure how to implement it into my code.
我已经用谷歌搜索了这个,但我仍然对如何使用它感到困惑。我正在制作一个文件管理器,我希望能够将文件复制并粘贴到新目录中。我知道复制我需要使用file.copy()
,但我不确定如何将它实现到我的代码中。
I would like to do this using fstream.
我想使用 fstream 来做到这一点。
采纳答案by Thomas Russell
If you are using the Win32 API then consider looking into the functions CopyFile
or CopyFileEx
.
如果您使用的是 Win32 API,请考虑查看函数CopyFile
或CopyFileEx
.
You can use the first in a way similar to the following:
您可以以类似于以下方式使用第一个:
CopyFile( szFilePath.c_str(), szCopyPath.c_str(), FALSE );
This will copy the file found at the contents of szFilePath
to the contents of szCopyPath
, and will return FALSE
if the copy was unsuccessful. To find out more about why the function failed you can use the GetLastError()
function and then look up the error codes in the Microsoft Documentation.
这会将在 的内容中找到的文件复制szFilePath
到 的内容szCopyPath
,FALSE
如果复制不成功,将返回。要了解有关函数失败原因的更多信息,您可以使用该GetLastError()
函数,然后在 Microsoft 文档中查找错误代码。
回答by R3D3vil
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));
}
回答by UpAndAdam
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx
I don't know what you mean by copy and paste a file; that makes no sense. You can copy a file to another location and I assume that's what you are asking about.
我不知道您所说的复制和粘贴文件是什么意思;这是没有意义的。您可以将文件复制到另一个位置,我认为这就是您要问的。
回答by dieram3
Here is my implementation to copy a file, you should take a look at boost filesystem since that library will be part of the standard c++ library.
这是我复制文件的实现,您应该查看 boost 文件系统,因为该库将成为标准 C++ 库的一部分。
#include <fstream>
#include <memory>
//C++98 implementation, this function returns true if the copy was successful, false otherwise.
bool copy_file(const char* From, const char* To, std::size_t MaxBufferSize = 1048576)
{
std::ifstream is(From, std::ios_base::binary);
std::ofstream os(To, std::ios_base::binary);
std::pair<char*,std::ptrdiff_t> buffer;
buffer = std::get_temporary_buffer<char>(MaxBufferSize);
//Note that exception() == 0 in both file streams,
//so you will not have a memory leak in case of fail.
while(is.good() and os)
{
is.read(buffer.first, buffer.second);
os.write(buffer.first, is.gcount());
}
std::return_temporary_buffer(buffer.first);
if(os.fail()) return false;
if(is.eof()) return true;
return false;
}
#include <iostream>
int main()
{
bool CopyResult = copy_file("test.in","test.out");
std::boolalpha(std::cout);
std::cout << "Could it copy the file? " << CopyResult << '\n';
}
The answer of Nisarg looks nice, but that solution is slow.
Nisarg 的答案看起来不错,但该解决方案很慢。
回答by elmadj
In native C++, you can use:
在本机 C++ 中,您可以使用:
- The CopyFile function : http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx
- CopyFileEx function for more advanced options like progress notifications. http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852%28v=vs.85%29.aspx
- CopyFile 函数:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa363851%28v=vs.85%29.aspx
- CopyFileEx 函数用于更高级的选项,如进度通知。http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852%28v=vs.85%29.aspx
回答by Ali
System::IO::File::Copy("Old Path", "New Path");
System::IO::File::Copy("旧路径", "新路径");