CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2005
    Posts
    25

    problems with CFile

    CFile *ObitFile;

    ObitFile->Open(_T("C:\\test.html"), CFile::modeRead); // my prog crashes at this line.

    i get the error: Unhandled exception at 0x004136a8 in testapp3.exe: 0xC0000005: Access violation reading location 0x00000000.

    the file exists, i tried adding:
    int stat;
    CFileStatus status;

    stat = ObitFile->GetStatus(_T("C:\\test.html"), status);
    to make sure i wasn't spelling something wrong, but that returns 1.

    Any ideas?

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Thumbs up Re: problems with CFile

    Quote Originally Posted by dave18285
    CFile *ObitFile;

    ObitFile->Open(_T("C:\\test.html"), CFile::modeRead); // my prog crashes at this line.
    You have forgotten to instantiate the pointer ObitFile.
    Correct code:
    CFile *ObitFile = new CFile ();

  3. #3
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    101

    Talking Re: problems with CFile

    Hi,
    You wrote:
    CFile *ObitFile;

    ObitFile->Open......

    'ObitFile' is a pointer and was never initialized.....
    you cant access it's members without initializing it first.

    BabyGuru.

  4. #4
    Join Date
    May 2005
    Posts
    25

    Re: problems with CFile

    I added:
    ObitFile = new CFile();

    and now i get:
    Debug Assertion Failed!
    File: filecore.cpp
    Line: 252

    I checked and line 252 says:
    ASSERT(lpBuf != NULL);
    ASSERT(AfxIsValidAddress(lpBuf, nCount));

    any ideas?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: problems with CFile

    Quote Originally Posted by dave18285
    I added:
    ObitFile = new CFile();

    and now i get:
    Debug Assertion Failed!
    File: filecore.cpp
    Line: 252

    I checked and line 252 says:
    ASSERT(lpBuf != NULL);
    ASSERT(AfxIsValidAddress(lpBuf, nCount));

    any ideas?
    Are you sure you posting your complete code? The assertion you get looks like it's in the CFile::Read() code

    Code:
     // From FileCore.cpp
    UINT CFile::Read(void* lpBuf, UINT nCount)
    {
      ASSERT_VALID(this);
      ASSERT(m_hFile != INVALID_HANDLE_VALUE);
      if (nCount == 0)
    	return 0; // avoid Win32 "null-read"
     
    ASSERT(lpBuf != NULL);
      ASSERT(AfxIsValidAddress(lpBuf, nCount));
      DWORD dwRead;
      if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
    	CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
      
      return (UINT)dwRead;
    }
    
    If you haven't please post the complete code.

    Btw, there really isn't any need to declare CFile on the heap (that way, you won't forget to delete it by chance).

    All you need is to open and read the file is:
    Code:
    CFile file;
    CFileException exc;
     
    if( file.Open( _T("C:\\Boot.ini") , CFile::modeRead, &exc ) )
    {
      TCHAR szBuffer[MAX_PATH + 1] = {0};
      DWORD dwRead = 0;
     
      do
    {
    	// Read a chunk of the file at a time
    if( 0 != (dwRead = file.Read( szBuffer, MAX_PATH )) )
    	{
    	  // Add a terminating null to the chunk we've read
    	  // so we can display it
    szBuffer[dwRead] = _T('\0');
    	  TRACE( _T("%s\n"), szBuffer );
    	}
      }
      while( dwRead > 0 );
     
      file.Close();
    }
    
    For a more complete example, see CFile::Open in msdn.

    Arjay

  6. #6
    Join Date
    May 2005
    Posts
    25

    Re: problems with CFile

    Code:
    CFile file;
    CFileException exc;
     
    if( file.Open( _T("C:\\Boot.ini") , CFile::modeRead, &exc ) )
    {
      TCHAR szBuffer[MAX_PATH + 1] = {0};
      DWORD dwRead = 0;
     
      do
    {
    	// Read a chunk of the file at a time
    if( 0 != (dwRead = file.Read( szBuffer, MAX_PATH )) )
    	{
    	  // Add a terminating null to the chunk we've read
    	  // so we can display it
    szBuffer[dwRead] = _T('\0');
    	  TRACE( _T("%s\n"), szBuffer );
    	}
      }
      while( dwRead > 0 );
     
      file.Close();
    }
    
    Thanks for the help, I tried this and it works now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured