Article ID: 152319
Article Last Modified on 11/21/2006
BOOL CStdioFile::ReadString(CString& rString)
{
ASSERT_VALID(this);
rString = &afxChNil; // empty string without deallocating
const int nMaxSize = 128;
LPTSTR lpsz = rString.GetBuffer(nMaxSize);
LPTSTR lpszResult;
int nLen;
for (;;)
{
lpszResult = _fgetts(lpsz, nMaxSize+1, m_pStream);
rString.ReleaseBuffer();
// handle error/eof case
if (lpszResult == NULL && !feof(m_pStream))
{
clearerr(m_pStream);
AfxThrowFileException(CFileException::generic, _doserrno,
m_strFileName);
}
// if string is read completely or EOF
if (lpszResult == NULL ||
(nLen = lstrlen(lpsz)) < nMaxSize ||
lpsz[nLen-1] == '\n')
{
break;
}
nLen = rString.GetLength();
lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
}
// remove '\n' from end of string if present
lpsz = rString.GetBuffer(0);
nLen = rString.GetLength();
if (nLen != 0 && lpsz[nLen-1] == '\n')
rString.GetBufferSetLength(nLen-1);
return lpszResult != NULL;
}
void ReadStringBug()
{
CString strBuffer;
CStdioFile sfMyFile( "C:\\test.txt", CFile::modeRead );
sfMyFile.ReadString( strBuffer );
AfxMessageBox( strBuffer );
sfMyFile.ReadString( strBuffer );
AfxMessageBox( strBuffer );
}
You must supply a file (titled c:\test.txt in the example) that contains at
least two strings with more than 128 characters in each string for the
problem to be manifested.
Additional query words: 4.00
Keywords: kbbug kbfileio kbfix kbnoupdate kbvc410fix KB152319