A way to read data out of a file at compile time to put it somewhere in application image files to initialize an array

前端 未结 4 1712
我在风中等你
我在风中等你 2021-01-29 03:21

considering visual C++ compiler, Lets say I\'ve got a file with whatever extension and it contains 100 bytes of data which are exactly the data that I want to initialize an arra

相关标签:
4条回答
  • 2021-01-29 03:46

    What I frequently use in testing for textual data is I create a std::istringstream object containing the text of the file to be read like this:

    #include <string>
    #include <fstream>
    #include <sstream>
    
    // raw string literal for easy pasting in
    // of textual file data
    std::istringstream internal_config(R"~(
    
    # Config file
    
    host: wibble.org
    port: 7334
    // etc....
    
    )~");
    
    // std::istream& can receive either an ifstream or an istringstream
    void read_config(std::istream& is)
    {
        std::string line;
        while(std::getline(is, line))
        {
            // do stuff
        }
    }
    
    int main(int argc, char* argv[])
    {
        // did the user pass a filename to use?
        std::string filename;
        if(argc > 2 && std::string(argv[1]) == "--config")
            filename = argv[2];
    
        // if so try to use the file
        std::ifstream ifs;
        if(!filename.empty())
            ifs.open(filename);
    
        if(ifs.is_open())
            read_config(ifs);
        else
            read_config(internal_config); // file failed use internal config
    }
    
    0 讨论(0)
  • 2021-01-29 03:58
    #include "filename"
    

    or am I missing something obvious?

    0 讨论(0)
  • 2021-01-29 04:05
    • Write a program that reads the 100 byte data file and generates as output, a file, with c++ code/syntax for declaring an array with the 100 bytes in the file.
    • Include this new generated file(inline) in your main c++ file.
    • Call the c++ compiler on the main c++ file.
    0 讨论(0)
  • 2021-01-29 04:08

    You do this with a resource in a Windows program. Right-click the project, Add, Resource, Import. Give the custom resource type a name. Edit the resource ID, if necessary. Get a pointer to the resource data at runtime with FindResource and LoadResource.

    0 讨论(0)
提交回复
热议问题