简体   繁体   English

如何读取EXE字节,存储到char数组中,然后将字节写入新的EXE文件中?

[英]How to read EXE bytes, store into a char array, and then write the bytes into a new EXE file?

I am trying to read bytes from an EXE file, send the bytes via socket, and then write the bytes into another EXE file. 我正在尝试从EXE文件读取字节,通过套接字发送字节,然后将字节写入另一个EXE文件。 The problem is that when I write the bytes to a EXE file and then try to open the EXE, Windows throws me an error: 问题是,当我将字节写入EXE文件然后尝试打开EXE时,Windows抛出错误:

图片

Here is my code for reading the bytes from the original EXE file: 这是我的代码,用于从原始EXE文件读取字节:

if (newConnection == 0)
{
    std::cout << "Error accepting connection\n\n" << std::endl;
}
else
{
    std::cout << IP << " successfully connected to the client.\n\n" << std::endl;

    std::ifstream fl("C:\\readbyte.exe");
    fl.seekg(0, fl.end);
    int length = fl.tellg();
    char *buffer = new char[length];
    fl.seekg(0, fl.beg);
    fl.read((char*)buffer, length);
    fl.close();
    std::cout << buffer << std::endl;
    send(newConnection, (char*)buffer, length, 0);

}

And here is my code for writing the bytes to a new EXE file: 这是我将字节写入新EXE文件的代码:

if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) // if we are unable to connet
{
    MessageBoxA(NULL, "Failed to connect", "Error", MB_OK | MB_ICONERROR);

}
else
{
    std::cout << "Connected to server.\n" << std::endl;

    std::vector<char> file(11776);
    std::cout << "Test1" << std::endl;
    if (recv(Connection, (char*)&file[0], file.size(), 0) < 0)
    {
        std::cout << "Test2" << std::endl;
        puts("Recv failed\n");
        system("pause");
        return -1;
    }
    else
    {
        std::cout << "Test3" << std::endl;
        std::cout << "File recieved: " << std::endl;

        /*for (int i = 0; i < file.size(); i++)
        {
            std::cout << file[i];
            if (int(file[i]) == 0)
            {
                file.erase(file.begin() + i);
            }

        }*/
        const char *path = "C:/Users/Public/writefile.exe";
        std::ofstream fout(path, std::ios::binary | std::ios::out);
        fout.flush();
        fout.write((char*)&file[0], file.size());
        fout.close();


        std::cout << "File size is: " << file.size() << std::endl;

        closesocket(Connection);
    }

I'm not sure what I am doing wrong. 我不确定自己在做什么错。 I've been working on this for 2 days now. 我已经为此工作了2天。

EDIT: ANSWER 编辑:答案

std::ifstream fl("C:\\readbyte.exe", std::ios::out | std::ios::binary);

I forgot to include std::ios::out | std::ios::binary 我忘了包括std::ios::out | std::ios::binary std::ios::out | std::ios::binary in the server ifstream . 服务器ifstream中的std::ios::out | std::ios::binary I also switched the out and binary arguments around. 我还切换了out和binary参数。

以二进制模式打开文件(将std::ios::binary作为ifstream第二个参数)

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

相关问题 如何在 C++ 中将 .mp4 文件读取为字节并将其存储在数组中 - How to read .mp4 file as bytes in c++ and store it in an array C++ - 将任何文件的字节读入无符号字符数组 - C++ - Read the bytes of any file into an unsigned char array 读取文件为字节并存储到确定性的8位值数组中 - Read file as bytes and store into an array of deterministically 8-bit values 读取char数组中的n个字节,并将其作为双精度值返回 - Read n bytes in a char array and return it as a double 数组中的 double 和数组中的 char 有多少字节? - How much bytes is a double in an array and char in an array? 从文件中读取和写入字节(c ++) - Read and write bytes from a file (c++) 在c ++中以字节为单位读取和写入文件 - read and write file in chunks of bytes in c++ 对于大小为 20 的 char[],如果我将 char 从 char[0] 输入到 char[7],然后将 char[] 写入文件,它会占用磁盘上的 20 个字节还是 8 个字节? - For a char[] of size 20, if i enter char from char[0] to char[7], and i write the char[] to a file, does it take up 20 bytes or 8 bytes on the disk? 如何使用 std::ifstream 从文件读取字节到 std::array? - How to read bytes from file using std::ifstream to std::array? 如何从无符号变量中写入和读取字节 - How to write and read bytes from unsigned variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM