inserting a text file into the resource of the mfc and then to be able to read from that file..

Posted on 2003-02-26
Medium Priority
Last Modified: 2013-11-20
I have a text file through which i have to read data..
i want this file to be a part of my resouce file so that
it is autpmatically incorporated in the .exe formed..
and not be a part of the disk wherever the exe is run..


Question by:canus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

jackrabbit22 earned 80 total points
ID: 8030371
In ResourceView, right-click on the topmost folder, and select Import from the popup menu.  Select the text file you want to import, and then call the new resource type TEXTFILE.  By default, the new resource should have the resource ID IDR_TEXTFILE1.

To load and read the text file: (may not be optimized, check help file for details)

     // Find the text file resource
     HRSRC hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_TEXTFILE1), "TEXTFILE");
     if (hSource)
          // Get the size of the text file (if need be)
          //DWORD dwSize = SizeofResource(AfxGetResourceHandle(), hSource);

          // Load the resource
          HGLOBAL hGlobal = LoadResource(AfxGetResourceHandle(), hSource);

          if (hGlobal)
               // Locking resource gives us access to the data
               char* pFile = (char*)LockResource(hGlobal);

               // Use a CString to make things easy to work with
               CString strTemp(pFile);
               // Do whatever

               // Free the resource

Expert Comment

ID: 8227201

bool WriteFromResource(LPSTR writeFileName, LPSTR resStr, LPSTR resId)

     BOOL bResult;
      HANDLE hRes;
     HRSRC hFound;
     void *lpBuff;
     HFILE filePtr =NULL;
     unsigned long bytesWritten ;
     long FileSize=0;

    hFound = FindResource( GetModuleHandle(NULL),  resId , resStr);
     if ( hFound == NULL )
          return 0;

     int kk = GetLastError();

    hRes = LoadResource( GetModuleHandle(NULL) , (HRSRC ) hFound);
     if ( hRes == NULL )
          return 0;

     FileSize =     SizeofResource( GetModuleHandle(NULL) , (HRSRC ) hFound);
    lpBuff = LockResource(hRes);
     if( GetFileAttributes( writeFileName ) != -1 ) return FALSE;
     filePtr = ( HFILE ) CreateFile( writeFileName , GENERIC_WRITE ,
                    0 , NULL , CREATE_NEW , FILE_ATTRIBUTE_NORMAL , NULL );
     if( filePtr == NULL )
          return false ;

     if( !WriteFile( (HANDLE ) filePtr , lpBuff ,  FileSize ,  &bytesWritten , NULL ))
          return false;

     CloseHandle( (HANDLE) filePtr );

    bResult = UnlockResource(hRes);
    bResult = FreeResource(hRes);

     return true;



WriteFromResource("c:\\filename.txt", "TXT_FILE" , MAKEINTRESOURCE( IDR_TXT_FILE) );

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question