I have a C++ Windows program. I have a text file that has some data. Currently, the text file is a separate file, and it is loaded at runtime and parsed. How is it possible to embed this into the binary as a resource?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you’re working on a native Windows application, what you want to do is to create a user-defined resource to embed the contents of the text file into the compiled resource.
The format of a user-defined resource is documented on MSDN, as are the functions for loading it.
You embed your text file in a resource file like this:
where
nameIDis some unique 16-bit unsigned integer that identifies the resource andtypeIDis some unique 16-bit unsigned integer greater than 255 that identifies the resource type (you may define those integers in theresource.hfile).filenameis the path to the file that you want to embed its binary contents into the compiled resource.So you might have it like this:
In
resource.h:In your resource file:
Then you load it like this (error-checking code omitted for clarity):
Note that you don’t actually have to free the resource since the resource resides in the binary of the executable and the system will delete them automatically when the program exits (the function
FreeResource()does nothing on 32-bit and 64-bit Windows systems).Because the data resides in the executable binary, you can’t modify it via the retrieved pointer directly (that’s why the
LoadFileInResource()function implementation stores the pointer in aconst char*). You need to use theBeginUpdateResource(),UpdateResource(), andEndUpdateResource()functions to do that.