Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 827695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:36:49+00:00 2026-05-15T03:36:49+00:00

I have a C++ Windows program. I have a text file that has some

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:36:49+00:00Added an answer on May 15, 2026 at 3:36 am

    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:

    nameID typeID filename
    

    where nameID is some unique 16-bit unsigned integer that identifies the resource and typeID is some unique 16-bit unsigned integer greater than 255 that identifies the resource type (you may define those integers in the resource.h file). filename is 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:

    // Other defines...
    
    #define TEXTFILE        256
    #define IDR_MYTEXTFILE  101
    

    In your resource file:

    #include "resource.h"
    
    // Other resource statements...
    
    IDR_MYTEXTFILE TEXTFILE "mytextfile.txt"
    

    Then you load it like this (error-checking code omitted for clarity):

    #include <windows.h>
    #include <cstdio>
    #include "resource.h"
    
    void LoadFileInResource(int name, int type, DWORD& size, const char*& data)
    {
        HMODULE handle = ::GetModuleHandle(NULL);
        HRSRC rc = ::FindResource(handle, MAKEINTRESOURCE(name),
            MAKEINTRESOURCE(type));
        HGLOBAL rcData = ::LoadResource(handle, rc);
        size = ::SizeofResource(handle, rc);
        data = static_cast<const char*>(::LockResource(rcData));
    }
    
    // Usage example
    int main()
    {
        DWORD size = 0;
        const char* data = NULL;
        LoadFileInResource(IDR_MYTEXTFILE, TEXTFILE, size, data);
        /* Access bytes in data - here's a simple example involving text output*/
        // The text stored in the resource might not be NULL terminated.
        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
        delete[] buffer;
        return 0;
    }
    

    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 a const char*). You need to use the BeginUpdateResource(), UpdateResource(), and EndUpdateResource() functions to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Give the form an onsubmit event. Have that event return… May 15, 2026 at 4:54 am
  • Editorial Team
    Editorial Team added an answer Use this my tool http://svncompletesync.codeplex.com/ or take it as a… May 15, 2026 at 4:54 am
  • Editorial Team
    Editorial Team added an answer The easiest solution here is often to use some form… May 15, 2026 at 4:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.