Thread: Reading Certain number of bytes from a file

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Unhappy Reading Certain number of bytes from a file

    Hi,

    I have just written this code:

    Code:
    case WM_CREATE:
    	{
    hFile = CreateFile("c:\\file.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 
    				   FILE_ATTRIBUTE_NORMAL, NULL);
    	}
    
    break;
    
    case WM_LBUTTONDOWN:
    	{
    
    char szMsg[128];
    
    if(GetFileType(hFile) == FILE_TYPE_DISK)
    {
    dwSize = GetFileSize(hFile, NULL);
    wsprintf(szMsg, "File Size = %ld", dwSize);
    }
    
    else
    {
    strcpy(szMsg, "Error");
    }
    
    MessageBox(hWnd, szMsg, "File Size In Bytes", NULL);
    
    
    ReadFile(hFile, szBuf, dwSize, &dwRead, NULL);
    szBuf[dwRead] = 0;
    
    MessageBox(hWnd, szBuf, "File Contense", NULL);
    	}
    
    break;

    Is is meant to get the file size of c:\file.txt and then using Read file read in the number of bytes returned by GetFileSize(). At the moment GetFileSize() returns 3114 as the number of bytes of file.txt which is correct. Then when i try to read it in my second message box is empty......

    So the first message box dispays the number of bytes which it does and the second is meant to display the files contense, which it dosnt.

    If i change dwsize in ReadFile() to somthing like 200 then it reads in some of the file, not all obviosly as it needs to read 3114 bytes. But if i put 3114 in rather that dwSize then the msgbox is empty again, as when using dwSize.

    Does anyone know what im doing wrong here?

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    does the file contain any null characters, is it a binary file? if the file contains a byte amounting to 0 i dont think a message box will print it beyond that point since they're made for strings.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey,

    No-one it is simply a text file with the word hello about 400 times. I think it should display in a message box ok. Maybe im wrong? Or is it my code.

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Long shot as I know you know what your doing, but is szBuf big enough to hold the read data?.....is there any possibility of a buffer overrun?

    When using ReadFile, I like to use dynamically allocated data with Malloc, new or (best for WinAPI) GlobalAlloc...

    You already have the size of the file in dwSize, so call GlobalAlloc with (dwSize + 1), and then cast it to a char* pointer.

    Read your data in and then set the position you got from dwRead to '\0'................

    You might have already done this...I have no way of knowing.....but I thought it might be worth mentioning

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Can MessageBox take a string >3K long?

    Try to print a small amount of the data into a test string and display that.

    Make sure that the file has not been corrupted, there is no close file in your code example. Best to open and close the file as needed. Do not leave data files open in case your app crashes and destroys the data.

    Use SetFilePointer() to ensure the file is read from the begining and catch the return from ReadFile().


    Code:
    #define    MBE          MB_ICONERROR|MB_OK
    char       szAppError[]={"My App's Error"};
    
    SetFilePointer(hFile, 0 ,0, FILE_BEGIN);
    iReturn=ReadFile(hFile,&TempString,sizeof(char)*dwSize,&NumWritten,NULL);
    if( (NumWritten==0)&&(iReturn==0) )//error
    {
        iError=GetLastError();
        sprintf(sErrorBuffer,"%s file failed to read with Error #:%d",sTextFileName,iError);
        MessageBox(hDlg,sErrorBuffer,szAppError,MBE);
        CloseHandle(hFile);
        return	INVALID;
    }
    else if( (NumWritten==0)&&(iReturn!=0) )//EOF
    {
        CloseHandle(hFile);
        return FALSE;}
    }
    else if( (NumWritten==dwSize)&&(iReturn!=0) )//not EOF
    (I used sizeof(char)*dwSize for demo, not needed with char's)
    Last edited by novacain; 01-13-2002 at 10:49 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey,

    Thanks guys i got it working for some reason it was just the message box not displaying it it was actually in the buffer.


    Cheers,

    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Yea. I thought a 3K string would be too big for the Msgbox. Lucky it handled it gracefully. (well if it did not you may have found the error sooner)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-05-2009, 10:16 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. reading a number from a file
    By the_head in forum C Programming
    Replies: 2
    Last Post: 10-02-2003, 09:25 PM