-
May 14th, 2005, 03:16 PM
#1
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?
-
May 14th, 2005, 03:30 PM
#2
Re: problems with CFile
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 ();
-
May 14th, 2005, 03:31 PM
#3
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.
-
May 14th, 2005, 03:46 PM
#4
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?
-
May 14th, 2005, 08:51 PM
#5
Re: problems with CFile
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
-
May 14th, 2005, 10:42 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|