Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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
5
Medium Priority
?
503 Views
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..

Thanx

0
Comment
Question by:canus
2 Comments
 
LVL 1

Accepted Solution

by:
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
               FreeResource(hGlobal);
          }
     }
0
 

Expert Comment

by:streetsmart
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;


}


call

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

578 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