English 中文(简体)
如何在C++中提供可起诉的副本?
原标题:How to make copies of the executable itself in C++?
  • 时间:2022-09-10 13:08:27
  •  标签:
  • c++
  • fstream

I want to make copies of an executable file itself multiple times.

I tried the following code:

#include <fstream>
#include <string>

int main() {
    std::ifstream from("main.exe", std::ios::binary);
    auto buf { from.rdbuf() };

    for(int x { 0 }; x <= 10; ++x) {
        std::string name { "main" + std::to_string(x) + ".exe" };

        std::ofstream out(name, std::ios::binary);
        out << buf;
        out.close();
    }

    from.close();
    return 0;
}

但是,它没有像我所期望的那样做工作(它没有重复复制可执行的工作)。 见以下屏幕上的规模一栏:

“The

我如何解决这一问题?

最佳回答

从投入文件流中读取数据。 你们需要在复制档案后重新确定上游:

...

for (int x{ 0 }; x <= 10; ++x) {
    std::string name{ "main" + std::to_string(x) + ".exe" };

    std::ofstream out(name, std::ios::binary);
    out << buf;
    out.close();
    from.seekg(0, std::ios::beg); // need to go back to the start here
}

...

您只能使用<代码>std:filesystem。 图书馆在这方面的标准功能:


int main() {
    std::filesystem::path input("main.exe");

    for (int x{ 0 }; x <= 10; ++x) {
        std::filesystem::path outfile("main" + std::to_string(x) + ".exe");

        std::filesystem::copy_file(input, outfile, std::filesystem::copy_options::overwrite_existing);
    }

    return 0;
}
问题回答

在您第一次访问时,你读过了缓冲投入的全部内容之后,你的投入缓冲是空的。 因此,您随后的通报也将提供空文。 我建议将<代码>从上移至像样的休息室内:

#include <fstream>
#include <string>

int main() {

    for (int x{ 0 }; x <= 10; ++x) {
        std::ifstream from("main.exe", std::ios::binary);
        auto buf{ from.rdbuf() };
        std::string name{ "main" + std::to_string(x) + ".exe" };

        std::ofstream out(name, std::ios::binary);
        out << buf;
        out.close();
        from.close();
    }

    
    return 0;
}

Here s what you should get: enter image description here

In the other answers the data will be read from the file multiple times (once per save).
This will happen if you create a new std::ifstream object inside the loop, and also happen if you use seekg to go to the beginning of the stream.

由于磁盘访问相对缓慢(与记忆存取相比),将 提高效率改为,然后将 射入<>/strong> ,即 视需要

下面的代码将文档本体的内容改为<编码>std:vector。 然后多次撰写病媒的内容,以创建新的档案:

#include <string>
#include <vector>
#include <fstream>

int main() {

    // Read the entire main.exe file into memory:
    const std::string inFilename{ "main.exe" };
    std::ifstream inFile{ inFilename, std::ios_base::binary };
    std::vector<char> inFileData{ std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>() };
    inFile.close();

    // Save it as many times as needed:
    for (int x{ 0 }; x <= 10; ++x) 
    {
        std::string name{ "main" + std::to_string(x) + ".exe" };
        std::ofstream outFile{ name, std::ios::binary };
        outFile.write(inFileData.data(), inFileData.size());
        outFile.close();
    }
}

第一份副本是完美的。 我最好的猜测是,投入的缓冲随后是空的,因此所有连续复制件都是空的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?