Home > Article > Backend Development > How to Embed Text Files as Resources in Native Windows Applications?
Embed Text File as a Resource in Native Windows Applications
In your C Windows application, you can embed a text file as a resource to avoid having it as a separate file that's loaded and parsed at runtime.
Creating a User-Defined Resource
To embed a text file, create a user-defined resource with the following format in a resource file:
nameID typeID filename
where:
For example:
#define TEXTFILE 256 #define IDR_MYTEXTFILE 101 IDR_MYTEXTFILE TEXTFILE "mytextfile.txt"
Loading the Resource
To load the embedded text file, use the following code:
DWORD size = 0; const char* data = NULL; LoadFileInResource(IDR_MYTEXTFILE, TEXTFILE, size, data);
where:
Access the Contents of the Embedded Text File
The data pointer points to the contents of the embedded text file. You can access the text as follows:
char* buffer = new char[size+1]; ::memcpy(buffer, data, size); buffer[size] = 0; // NULL terminator ::printf("Contents of text file: %s\n", buffer); // Print as ASCII text
Limitations
Note that the data in the embedded text file is constant and cannot be modified directly through the retrieved pointer. To modify it, use the BeginUpdateResource(), UpdateResource(), and EndUpdateResource() functions.
The above is the detailed content of How to Embed Text Files as Resources in Native Windows Applications?. For more information, please follow other related articles on the PHP Chinese website!