ReadFile() Putting Trash in Buffer

This is a discussion on ReadFile() Putting Trash in Buffer within the Windows Programming forums, part of the Platform Specific Boards category; Hey, I had made a simple window using the Forgers tutorials. Now I'm making a program that needs a window, ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    ReadFile() Putting Trash in Buffer

    Hey,

    I had made a simple window using the Forgers tutorials. Now I'm making a program that needs a window, but its in c++ rather then c (as the tutorials are). I was able to fix all of the problems except one when I converted the code, and it is in the readfile() function.

    Anyway, after messing with it for a while and reading th documentation in it, I got it to compile:
    Code:
    void LoadTextFileToEdit(HWND hEdit, HWND window, LPCTSTR pszFileName)
    {
        LPCTSTR  lpBuffer;
        LPDWORD dwNumRead;
        DWORD size;
        
        size = sizeof(DWORD)*256;
        dwNumRead = &size;
        
        ReadFile(&pszFileName, &lpBuffer, size, dwNumRead,NULL);
        SetWindowText(window, pszFileName);
        SetWindowText(hEdit, lpBuffer);
    }
    This code will run but the contents of the file is not being read into lpBuffer, or at least not properly, because when I open a file it spits out stuff like:
    USVuW

    I know that the problem is me messing around with code I don't really understand, but if it's not too much trouble could you point out the problem.

    Thanks,
    David

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Well, the first parm to ReadFile should be a HANDLE for an open file not the file name. You should always check your return codes.

    Code:
    BOOL LoadFile( LPSTR pszFileName)
    {
        HANDLE hFile;
        BOOL bSuccess = FALSE;
    
        hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
            OPEN_EXISTING, NULL, NULL);
        if(hFile != INVALID_HANDLE_VALUE)
        {
            DWORD dwFileSize;
            dwFileSize = GetFileSize(hFile, NULL);
            if(dwFileSize != 0xFFFFFFFF)
            {
                LPSTR pszFileText;
                pszFileText =(LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);
                if(pszFileText != NULL)
                {
                    DWORD dwRead;
                    if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                    {
                        pszFileText[dwFileSize] = 0; // Null terminator
                        bSuccess = TRUE; // It actually worked!!!!
                    }
                    GlobalFree(pszFileText);
                }
            }
            CloseHandle(hFile);
        }
        return bSuccess;
    }

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Yeah, I actaully forgot to do createfile(). Thanks.

    I'm getting an error that doesn't make much since now:
    147 ld returned 1 exit status

    147 is the create file function:
    Code:
    hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
            OPEN_EXISTING, NULL, NULL);
    EDIT: ^Disregard, it was a spelling mistake.

    And thanks again.
    Last edited by IdioticCreation; 01-18-2007 at 05:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM
  5. Putting CView text into a buffer
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2002, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21