Thread: Store txt file in program

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    Store txt file in program

    hey...i was wondering, can i somehow store a text file in my program? that way if the user wanted to, they could restore the original file. i dont want to do a zip exe because this is a game, but yea...otherwise i'm going to have to create a rather large 2 dim array, lol. thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Embed your text file in a resource file

    Create a resource file with the following assuming new.txt is the name of your text file:
    Code:
    #define IDR_MYNEW 		1000
    
    IDR_MYNEW             MYNEW_TXT DISCARDABLE     "C:\\temp\\new.txt"
    Assuming myRC.RC is the name of your resource file
    Compile the resource file: RC myRC.RC

    Assuming my.cpp is the name of your source file, then:
    cl my.cpp myRC.res

    In your my.cpp execute the following functions to extract the embedded text file named new.txt:

    FindResource (find embedded text file in resource file)
    LoadResource
    LockResource
    //Now copy embedded text file to hard drive
    CreateFile
    WriteFile
    Last edited by BobS0327; 01-30-2006 at 03:04 PM. Reason: clarify statement, change embeded executable to embedded text file

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    sweet.

    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i dont think i understand how to copy the embedded text file to the hard drive. this is what i have so far, but i'm pretty sure theres a bit wrong...
    Code:
    res.h: #define ID_TXTFILE 500
    res.rc: ID_TXTFILE  TXTFILE_TXT DISCARDABLE     "ext.txt"
    main.c: case ID_TOOLS_REPLACE:
                     {
                          HRSRC rsTxt;
                          HANDLE hExe;
                          HGLOBAL glTxt;
                          char *lpLock;
                          
                          // Load the .EXE file that contains the file to copy. 
                          hExe = LoadLibrary("FindText.exe");
                          if(hExe == NULL)
                          {
                              MessageBox(NULL, "There was an error accessing the extensions file.",
                                "Error!", MB_OK | MB_ICONEXCLAMATION);
                          }
                          
                          rsTxt = FindResource(hExe, "ext.txt", RT_RCDATA);
                          if(rsTxt != NULL)
                          {  
                              glTxt = LoadResource(hExe, rsTxt);
                              if(glTxt != NULL)
                              {
                                  lpLock = LockResource(glTxt);
                                  if (lpLock = NULL)
                                      MessageBox(NULL, "Could not lock extensions file.", "Error!", MB_OK | MB_ICONEXCLAMATION);
                                  
                                  HANDLE hFile;
                                  BOOL bSuccess = FALSE;
                                  TCHAR fileName[8] = "ext.txt";
                                  DWORD dwWritten;
    
                                  hFile = CreateFile(fileName, GENERIC_WRITE, 0, 0,
                                     CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY, 0);
                                  if(hFile != INVALID_HANDLE_VALUE)
                                  {
                                     if(WriteFile(hFile, glTxt, (DWORD)strlen(glTxt), &dwWritten, NULL))
                                         bSuccess = TRUE;
                                  }
                               }
                          }
                          // Clean up. 
                          if (!FreeLibrary(hExe)) 
                          { 
                              MessageBox(NULL, "Could not free extensions file.", "Error!", MB_OK | MB_ICONEXCLAMATION); 
                          } 
                     }
                     break;
    help? please? lol thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'm wondering about the strlen thing. I'd try SizeofResource(..)

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i changed it to use that but the file still isn't being written. ??
    Last edited by willc0de4food; 04-16-2006 at 01:07 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    if (lpLock = NULL)
    if (lpLock == NULL)
    And then write the lpLock, not glTxt (which is just a handle to the resource)

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i modified that and it still wasn't working, so i added some messagebox's for errors and now it wont compile.
    Code:
    case ID_TOOLS_REPLACE:
                     {
                          HRSRC rsTxt;
                          HANDLE hExe;
                          HGLOBAL glTxt;
                          char *lpLock;
                          
                          // Load the .EXE file that contains the file to copy. 
                          hExe = LoadLibrary("FindText.exe");
                          if(hExe == NULL)
                          {
                              MessageBox(NULL, "There was an error accessing the extensions file.",
                                "Error!", MB_OK | MB_ICONEXCLAMATION);
                          }
                          
                          rsTxt = FindResource(hExe, "ext.txt", RT_RCDATA);
                          if(rsTxt != NULL)
                          {  
                              glTxt = LoadResource(hExe, rsTxt);
                              if(glTxt != NULL)
                              {
                                  lpLock = LockResource(glTxt);
                                  if (lpLock == NULL)
                                      MessageBox(NULL, "Could not lock extensions file.", "Error!", MB_OK | MB_ICONEXCLAMATION);
                                  
                                  HANDLE hFile;
                                  BOOL bSuccess = FALSE;
                                  TCHAR fileName[8] = "ext.txt";
                                  DWORD dwWritten;
    
                                  hFile = CreateFile(fileName, GENERIC_WRITE, 0, 0,
                                     CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY, 0);
                                  if(hFile != INVALID_HANDLE_VALUE)
                                  {
                                     if(WriteFile(hFile, lpLock, (DWORD)SizeofResource(hExe, rsTxt), &dwWritten, NULL))
                                     {
                                         MessageBox(NULL, "The file has been successfully been restored.", "Success",
                                           MB_OK | MB_ICONINFORMATION);
                                     }
                                     else
                                     {
                                         MessageBox(NULL, "There was an error restoring the file.", "Error",
                                           MB_OK | MB_ICONEXCLAMATION);
                                     }
                                  }
                                  else
                                      MessageBox(NULL, "hFile had an error", "ok" MB_OK);  
                               }
                               else
                                   MessageBox(NULL, "glTxt == NULL", "ok", MB_OK);
                          }
                          else
                              MessageBox(NULL, "rsTxt == NULL", "ok", MB_OK);
                          // Clean up. 
                          if (!FreeLibrary(hExe)) 
                          { 
                              MessageBox(NULL, "Could not free extensions file.", "Error!", MB_OK | MB_ICONEXCLAMATION); 
                          } 
                     }
                     break;
    it stops at the else, messagebox right after trying to write the file MessageBox(NULL, "hFile had an error", "ok" MB_OK);
    the error is "main.c: In function `WindowProcedure':
    main.c:397: error: syntax error before numeric constant"
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    A console example compiled with your resource file (res.rc)
    Code:
    #pragma comment( lib, "user32.lib" )   
    #include <windows.h>
    #include <winbase.h>
    #include <objbase.h>
    #include <shlobj.h>
    #include <stdio.h>
    #include "res.h"
    
    #define TXTFILE_TXT         "ext.txt"
    
    int main(void)
    {
        HGLOBAL glTxt;
        CHAR *lpLock;
        HANDLE hFile;
        HRSRC rsTxt;
        HMODULE hInstance;
        DWORD dwSizeofResource;
        CHAR szTextFilePath[MAX_PATH];
        DWORD dwWritten = 0;
        hInstance = GetModuleHandle(NULL);
        rsTxt = FindResource(hInstance, MAKEINTRESOURCE(ID_TXTFILE), "TXTFILE_TXT" );
        if(rsTxt != NULL)
        {  
            glTxt = LoadResource(hInstance, rsTxt);
            if(glTxt != NULL)
            {
                lpLock = LockResource(glTxt);
                if (lpLock == NULL)
                    return -1;
                dwSizeofResource = SizeofResource(hInstance, rsTxt);
                sprintf(szTextFilePath,("C:\\Temp\\%s"), "ext.txt");
                hFile = CreateFile( 
                    szTextFilePath,
                    GENERIC_WRITE,
                    0,
                    NULL,
                    CREATE_ALWAYS,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL );
                if (hFile == INVALID_HANDLE_VALUE)
                    return -1;
                WriteFile(hFile, lpLock, dwSizeofResource, &dwWritten, NULL);
                CloseHandle(hFile);
                return dwWritten == dwSizeofResource;
            }
            else
                return -1;
        }
        else
            return -1;
    }

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The problem in your code is just that

    Code:
    MessageBox(NULL, "hFile had an error", "ok" MB_OK);
    MessageBox(NULL, "hFile had an error", "ok", MB_OK);

  11. #11
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    YESS, you guys rock. lol the combination of the
    rsTxt = FindResource(hInstance, MAKEINTRESOURCE(ID_TXTFILE), "TXTFILE_TXT" );
    and the messagebox error b/c i'm dumb made it work like a charm.

    thanks guy
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM