Reputation: 2055
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 array of char data type with a length of 100 characters with, Now apparently one way is to read those data out of file by using I/O file classes or APIs at run-time but what I want to know is that, is there any way using directives or something to tell the compiler I want to put that data in a right place in my application image files at compile time and compiler should go read those data out of that file?
Upvotes: 1
Views: 3717
Reputation: 48635
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
}
Upvotes: 1
Reputation: 9148
Upvotes: 4
Reputation: 942257
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.
Upvotes: 2