繁体   English   中英

在 C++ 中,如何读取嵌入在可执行文件中的文件?

[英]In C++, how do you read a file that is embedded in an executable?

我有一张名为 photo.jpg 的照片。 我使用xxd -i为我的图像文件生成一个 C++ 文件。 而 output 是这样的:

unsigned char photo_jpg[] = {

    0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,

    0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x84,...};

unsigned int photo_jpg_len = 24821;

我将该文件嵌入到我的 main.cpp 文件中并构建它以生成我的可执行文件。

我怎么可能从可执行文件中读取我的图像文件的十六进制转储部分?

我试过什么...?

我使用xxd myfile.exe > file.txt.

当我检查使用 xxd 生成的可执行文件的十六进制转储时,我发现 photo_jpg 字符数组的字节位于可执行文件中的某处。 您可以在下图中看到:这是我的可执行十六进制转储的屏幕截图。

我如何读取它并将其存储在字符数组中,就像嵌入到我的可执行文件之前一样?

起初,我将我的 zip 文件转换为一个字符串,它是用这个命令:

xxd -p archive.zip > myfile.txt

然后我使用这段代码在我的 zip 文件的十六进制转储的 C++ 中生成一个多行字符串:

#include <fstream>
#include <string>
int main()
{
    std::ifstream input("file.txt");
    std::ofstream output("string.txt");
    std::string double_coute = "\"";
    std::string line;
    while (getline(input, line))
    {
        std::string to_be_used = double_coute + line + double_coute + "\n" ;
        output << to_be_used;
    }
    output.close();
    input.close();
    return 0;
}

之后,我将 string.txt 的内容放入一个全局变量中。 使用下面的 function 我能够编写一个生成 zip 文件的程序。

void WriteHexDatatoFile(std::string &hex)
{
    std::basic_string<uint8_t> bytes;

    // Iterate over every pair of hex values in the input string (e.g. "18", "0f", ...)
    for (size_t i = 0; i < hex.length(); i += 2)
    {
        uint16_t byte;

        // Get current pair and store in nextbyte
        std::string nextbyte = hex.substr(i, 2);

        // Put the pair into an istringstream and stream it through std::hex for
        // conversion into an integer value.
        // This will calculate the byte value of your string-represented hex value.
        std::istringstream(nextbyte) >> std::hex >> byte;

        // As the stream above does not work with uint8 directly,
        // we have to cast it now.
        // As every pair can have a maximum value of "ff",
        // which is "11111111" (8 bits), we will not lose any information during this cast.
        // This line adds the current byte value to our final byte "array".
        bytes.push_back(static_cast<uint8_t>(byte));
    }

    // we are now generating a string obj from our bytes-"array"
    // this string object contains the non-human-readable binary byte values
    // therefore, simply reading it would yield a String like ".0n..:j..}p...?*8...3..x"
    // however, this is very useful to output it directly into a binary file like shown below
    std::string result(begin(bytes), end(bytes));
    std::ofstream output_file("khafan.zip", std::ios::binary | std::ios::out);
    if (output_file.is_open())
    {
        output_file << result;
        output_file.close();
    }
    else
    {
        std::cout << "Error could not create file." << std::endl;
    }
}

但有什么意义呢?

重点是您的 zip 文件中可以包含任何类型的资源。 通过这种方式,您可以用您的程序生成所需的资源并用它们做一些事情。 我认为这很酷。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM