Ask a questionAsk a question
 

QuestionMapViewOfFile - Mapping only few bytes

  • Wednesday, November 04, 2009 10:14 AMSyed Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi
    I have a file of size 74KB on my disk.
    I created file mapping object with size equal to the size of my file.

    //Create file object
        m_hFile    =    CreateFile( sFileName.operator LPCTSTR(),GENERIC_READ  ,FILE_SHARE_READ,
            NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );
       

        //Get size of the file
        DWORD dwFileSize = GetFileSize(m_hFile,NULL);
       
        //Create File mapping object
        m_hFileMapping1    = CreateFileMapping(m_hFile,NULL,PAGE_READONLY,0,dwFileSize,"MemMap1");

        //Map view of the file.I need to map only first 4096 bytes in my address space
        char* m_pStr = (char*) MapViewOfFile(m_hFileMapping1,FILE_MAP_READ,0,0,4096);

    MapViewOfFile is succeeded.But when i try to manipulate the m_pStr [ like finding the length of it, or displaying in message box ] crashes.
    I dont know where is the problem.
    Is this way to read  only few bytes [ 4096 bytes ] from the file?

    Can someone help me?

All Replies

  • Wednesday, November 04, 2009 3:03 PMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What error do you get?  Does the file contain character strings?  Are the character strings nul terminated?

    If the file contains binary data then casting the pointer to char* and using char functions is a mistake. String functions only work for nul terminated character strings.
  • Thursday, November 05, 2009 3:21 AMSyed Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Scott
    my file is a simple text file and it contains only characters.
    I didnt get any error.
    MapViewOfFile is successful but if i try to shw the m_pstr in MessageBox its crashing.
    When i debugged the string it holds the characters.
    I'm not sure that the string is null terminated or not.
  • Thursday, November 05, 2009 4:39 AMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Put a nul terminator at the end.

    m_pStr[dwFileSize] = '\0';

    before you do anything with m_pStr.  Any function that you pass m_pStr to will require the terminator, and if there is no terminator it will keep reading until it reaches an invalid address.

  • Thursday, November 05, 2009 4:51 AMSyed Babu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi scot
    If I try to access any byte of m_pStr is crashing.
    m_pStr[1] or m_pStr[50] , any index is crashing.
    But while debugging if i move the mouse over the m_pStr it showing the characters read.
  • Thursday, November 05, 2009 3:04 PMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Maybe your CreateFileMapping or MapViewOfFile call failed.  Check the return value from each. 

    Another probable problem is that you are opening the file mapping read only, but you need to write a nul into it if it does not have one, and you need to make it one char larger than the file so you will have a place to write that nul.

    You like the word "crashing" but it conveys no useful information.  Explain what specific symptom or error message happens that you call "crashing."