Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DimSum

CStdioFile::ReadString().... why is this function giving me HELL??

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I''m trying to simply read the first line from a txt file and i''m having problems using CStdioFile:ReadString() in Visual C++ to do it. It looks so simple, yet I keep getting a runtime error when ReadString() is called. It says some garbage: Unhandled exception at 0x77f83147 in MetricsProgram.exe: 0xC0000005: Access violation reading location 0x00000034. Here''s my code: CString strfnGetFirstLine () { CStdioFile inFile; CFileException efile; CString strFirstLine; // open sometext.txt if( !inFile.Open( "sometext.txt", CFile::modeRead | CFile::shareExclusive, &efile ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << efile.m_cause << "\n"; #endif return NULL; } // get first line inFile.ReadString(strFirstLine); // <-- Fails here! return strFirstLine; } Does anyone know what''s wrong??? thanks. -Phil

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Here''s where it fails.
It complains that "lpsz" wasn''t found. How do I fix this??
is LPTSTR lpsz = rString.GetBuffer(nMaxSize); not doing its thing??

Here''s the ReadString() function that it steps into:

BOOL CStdioFile::ReadString(CString& rString)
{
ASSERT_VALID(this);

rString = _T(""; // empty string without deallocating
const int nMaxSize = 128;
LPTSTR lpsz = rString.GetBuffer(nMaxSize);
LPTSTR lpszResult;
int nLen = 0;
for (;
{
// FAILS at this call. Because "lpsz" was not
// found. How do I fix this??
lpszResult = _fgetts(lpsz, nMaxSize+1,
m_pStream);
rString.ReleaseBuffer();

....
}

Does anyone see why its failing??

thanks.

-Phil

Share this post


Link to post
Share on other sites
Here''s where it fails.
It complains that "lpsz" wasn''t found. How do I fix this??
is LPTSTR lpsz = rString.GetBuffer(nMaxSize); not doing its thing??

Here''s the ReadString() function that it steps into:

BOOL CStdioFile::ReadString(CString& rString)
{
ASSERT_VALID(this);

rString = _T(""); // empty string without deallocating
const int nMaxSize = 128;
LPTSTR lpsz = rString.GetBuffer(nMaxSize);
LPTSTR lpszResult;
int nLen = 0;
for (;
{
// FAILS at this call. Because "lpsz" was not
// found. How do I fix this??
lpszResult = _fgetts(lpsz, nMaxSize+1,
m_pStream);
rString.ReleaseBuffer();

....
}

Does anyone see why its failing??

thanks.

-Phil

Share this post


Link to post
Share on other sites
LPTSTR == TCHAR*

The pointer needs to be malloc''d or new''d before use, or use a fixed size array

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites
No. I mean the function doesn''t allocate the memory (who would free it?), you have to. That''s what the max size argument is for.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites
According to The MSDN, the rString.GetBuffer(nMaxSize) function allocates a buffer though. So if that returns NULL, that must be where the problem is. Although i can't think why it would return NULL and not throw a CMemoryException

Edit: Typo



[edited by - Evil Bill on December 6, 2002 9:00:31 AM]

Share this post


Link to post
Share on other sites
Yup, sorry about the dodgy advice. It was a quick glance and thinking, "It must be the old you alloc the array problem".

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!