Thread: ReadFile, GlobalLock, etc questions

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    ReadFile, GlobalLock, etc questions

    Trying to read a file that has the basic structure of:

    <string> <string> <string> <int> x 9

    I am able to succesfully open the file using CreateFile. The probelm is trying to read the file. Since I can't find a fscanf() way of reading the blocks one by one I looked at the boards and it was suggested to read the entire file and then search the string to get the information I want.

    Here is my code (for this function) so far:

    Code:
    void getplayerinfo (void)
    {
    HANDLE hFile, hBuffer;
    DWORD numbytes;
    DWORD tmp;
    LPSTR szGlobBuffer;
    
    if ( (hFile = CreateFile ("Gnos.dat", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
          {
          MessageBoxPrintf(TEXT("Error Opening File"), TEXT("Error number %d"), GetLastError() );
          PostQuitMessage(0);
          }
    
    tmp = GetFileSize(hFile, NULL);
    
    hBuffer = GlobalAlloc ( GHND, tmp + 1);
    szGlobBuffer = GlobalLock ( (HGLOBAL) hBuffer);
    
    ReadFile ( hFile, szGlobBuffer, tmp, &numbytes, NULL);
    
    MessageBoxPrintf(TEXT("NAME"), TEXT("%d - %d"), (int) numbytes, (int) tmp);
    
    GlobalUnlock (hBuffer) ;
    GlobalFree (hBuffer) ;
    
    CloseHandle (hFile);
    }
    File data:
    Code:
    Gnos Twistor Game_Master 100 0 0 0 100 100 100 0 0 900000 0 1
    GlobalLock returns the error:
    ANSI C++ forbids implicit conversion from `void *' in assignment
    Compiler: Dev- C++
    Version: 4.9.8.1
    Platform: WinXP

    Any help would be great.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Sense each string does not contain any spaces you could load the file into a string and then parse that string. Here is an example I took from The Forgers Win32 API tutorial.
    Code:
    BOOL LoadTextFile(LPCTSTR pszFileName)
    {
        HANDLE hFile;
        BOOL bSuccess = FALSE;
        hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
            OPEN_EXISTING, 0, NULL);
        if(hFile != INVALID_HANDLE_VALUE)
        {
            DWORD dwFileSize;
            dwFileSize = GetFileSize(hFile, NULL);
            if(dwFileSize != 0xFFFFFFFF)
            {
                LPSTR pszFileText;
                pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
                if(pszFileText != NULL)
                {
                    DWORD dwRead;
                    if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                    {
                        pszFileText[dwFileSize] = 0; // Add null terminator
                        //The data is now in pszFileText
                            bSuccess = TRUE; // It worked!
                    }
                    GlobalFree(pszFileText);
                }
            }
            CloseHandle(hFile);
        }
        return bSuccess;
    }
    This example loads the data into pszFileText. Notice how it gets the file size and then allocates that much room. Where it says //The data is now in pszFileText. You can then go through and parse the string.

    You can advance through the string until you get to a space. Then you know you have reached the first string. Then copy that into another String where you want to save it. You would need to use strcpy or strncpy. Also take a look at sscanf.

    Hope that helps.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I tried your code and got the same response for GlobalLock
    ANSI C++ forbids implicit conversion from `void *' in assignment
    This is the sticking point it seems.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Perhaps I'm being thick here, (it is 5 in the morning), but why are you trying to use all of those old fashioned 16 bit compatibility crap routines anyway?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Do you have a better idea?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok to make this work I had to change the GlobalLock line to:
    Code:
    lpBuffer = (char *) GlobalLock (hGlobal);
    Why? Oh since the code has been redone here is what I have now:

    Code:
    void getplayerinfo (void)
    {
    int *p;
    GLOBALHANDLE hGlobal;
    HANDLE hFile;
    DWORD length;
    DWORD bytesRead;
    LPSTR lpBuffer;
    
    if ( (hFile = CreateFile("gnos.dat", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
       {
       MessageBoxPrintf(TEXT("ERROR"), TEXT("hFile error %d"), GetLastError() );
       PostQuitMessage(0);
       }
    
    length = GetFileSize (hFile, NULL);
    
    hGlobal = GlobalAlloc ( GHND, length + 1);
    
    lpBuffer = (char *) GlobalLock (hGlobal);
    
    ReadFile (hFile, lpBuffer, length, &bytesRead, NULL);
    
    MessageBoxPrintf(TEXT("ReadFile"), TEXT("Read: %d out of %d"), (int) bytesRead, length);
       
    MessageBoxPrintf(TEXT("String"), TEXT("%s"), lpBuffer);
    
    }

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Why?

    Because GlobalLock() returns a void pointer, the variable you are assigning is a char pointer. C++ does not like assigning a value of one type to a variable of another, hence you always need to cast the return value.

    Win32 does not have the concept of local and global heaps. These routines are still in the Win32 API so that old 16bit code doesn't break. They are not necessary in 32bit applications. Why can you not allocate your heap storage with new?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm still learning windows.

    I've gotten the file loaded into a string but I'm having a $$$$$ of a time extracting the parts and putting them where I want them and can use them.

    I'm about this >< close to scrapping it and using fscanf()

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    However you open and read your file, you will need to allocate your heap storage, do this with new. fscanf() is also a C legacy hangover of course.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    LPSTR szGlobBuffer;

    try

    Code:
    char* pGlobBuffer=NULL,pStart=NULL,pTemp=NULL;
    char   sText[255]= {0} ;
    
    //after you have the file in memory should be error checked
    
    pStart=pGlobBuffer; //set the pointer to the begining of the buffer
    while((pStart!='\0')
    {
         // first is a string so need space to put it
         pTemp=sText; //set the temp pointer  to the begining of the target string
         while( (*pStart != '\n') && (*pStart != '\r') && (*pStart != '\0') ) //while in the string
    		*pTemp++ = *pStart++; //copy to the target string
          pTemp='\0'; //terminate string
    
          // use the string 
    
          ZeroMemory(sTemp, 255); //clear string
    "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. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM