简体   繁体   English

如何使用 C++ 在 Windows 中复制和粘贴文件?

[英]How can I copy and paste a file in Windows using C++?

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 to 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 来做到这一点。

If you are using the Win32 API then consider looking into the functions CopyFile or CopyFileEx .如果您使用的是 Win32 API,请考虑查看函数CopyFileCopyFileEx

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.这会将在szFilePath的内容中找到的文件复制到szCopyPath的内容,如果复制不成功,将返回FALSE 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.要了解有关函数失败原因的更多信息,您可以使用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));
}

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 的答案看起来不错,但该解决方案很慢。

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.您可以将文件复制到另一个位置,我认为这就是您要问的。

In native C++, you can use:在本机 C++ 中,您可以使用:

Using C makes C++ projects more portable.使用 C 使 C++ 项目更具可移植性。 Here my sample code is a reference for you.这里我的示例代码供您参考。

int file_copy(char* fn_dst, char* fn_src) {
    FILE* pf = NULL;
    char* buf = NULL;   
    int len = 0;
    
    //get length of file src
    pf = fopen(fn_src, "r");
    if (pf == NULL) {
        printf("[%s]Failed to fopen:%s\n", __func__, fn_src);
        return -1;
    }
    fseek(pf, 0, SEEK_END);
    len = ftell(pf);
    fclose(pf);
    //allocate buf
    buf = (char*)malloc(len);
    if (buf == NULL) {
        printf("[%s]Failed to malloc\n", __func__);
        return -1;
    }
    memset(buf, 0, len);
    //read from file src
    pf = fopen(fn_src, "rb");
    if (pf == NULL) {
        printf("[%s]Failed to fopen:%s\n", __func__, fn_src);
        return -1;
    }
    fread(buf, 1, len, pf);
    fclose(pf);
    //write into file dst
    pf = fopen(fn_dst, "wb+");
    if (pf == NULL) {
        printf("[%s]Failed to fopen:%s\n", __func__, fn_dst);
        return -1;
    }
    fwrite(buf, 1, len, pf);
    fclose(pf);

    if (buf)
        free(buf);
    return 0;
}

System::IO::File::Copy("旧路径", "新路径");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++,我可以使用enable_if代替复制粘贴来启用多个特征类专业化吗? - c++, can I enable several trait class specializations using enable_if instead of copy-paste? C ++:没有复制和粘贴怎么办? 它本身循环 - C++ : How can I do it without copy&paste? It loops itself 我无法在 c++(代码块)中使用 fstream 将文件内容复制到另一个。 我怎样才能运行文件? - I can't copy file content to another using fstream in c++(codeblocks). How can I run the file? 我如何在 windows 7 - how can i run a c++ file in turbo c in windows 7 如何使用 c++ 从 windows 服务运行 a.exe 文件 - How can I run a .exe file from a windows service using c++ 可以从文本文件复制并粘贴到打开word程序的C++ - C++ that can copy from text file and paste to open word program QT C ++我可以使用Custom mime-type在多个应用程序上进行复制和粘贴吗? - QT C++ Can I use Custom mime-type for copy and paste on multiple application? c++ - 如何在c/c++中将文件从一个目录复制到另一个目录 - How can I copy a file from one directory to another in c/c++ 如何使用c ++在Windows中锁定文件? - How lock a file in windows using c++? 你如何在 C++ 中从剪贴板复制/粘贴? - How do you copy/paste from the clipboard in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM