15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Problems reading textfile with CStdioFile

stefan.o@monitor.se
Friday, January 10, 1997

Environment: NT 4.0 VC++ 4.0

I'm trying to read a textfile with code like this:
CString text;
CString textRow;
 CStdioFile file(filename, CFile::modeRead | CFile::typeText);=20
  while(file.ReadString(textRow))
  {
    text +=3D textRow;
  }

Some lines in the textfile are quite long (200 chars) and some shorter.
The first long line is read ok, but any long lines after that are truncated=
 to=20
128 chars even
if they are identical to the first one.

Am I doing someting wrong or is it a bug?

/Stefan






Mike Blaszczak -- mikeblas@nwlink.com
Saturday, January 11, 1997

[Mini-digest: 4 responses]

At 12:05 1/10/97 +0100, stefan.o@monitor.se wrote:
>Environment: NT 4.0 VC++ 4.0

>I'm trying to read a textfile with code like this:
>CString text;
>CString textRow;
> CStdioFile file(filename, CFile::modeRead | CFile::typeText); 
>  while(file.ReadString(textRow))
>  {
>    text += textRow;
>  }

>Some lines in the textfile are quite long (200 chars) and some shorter.
>The first long line is read ok, but any long lines after that
>are truncated to 128 chars even if they are identical to the first one.

>Am I doing someting wrong or is it a bug?

Unfortunately, it's a bug.  I think it was fixed in 4.1, but I'm not
at liberty to check.

If you're trying to read the entire content of a the file into a 
CString, though, you could certainly do so more efficiently using the
Read() member of CStdioFile.


.B ekiM
http://www.nwlink.com/~mikeblas/
Why does the "new" Corvette look like a 1993 RX-7?
These words are my own. I do not speak on behalf of Microsoft.

-----From: jeremy@omsys.com (Jeremy H. Griffith)

On Fri, 10 Jan 1997 12:05:07 +0100, stefan.o@monitor.se wrote:

>Environment: NT 4.0 VC++ 4.0
>
>I'm trying to read a textfile with code like this:
>CString text;
>CString textRow;
> CStdioFile file(filename, CFile::modeRead | CFile::typeText); 
>  while(file.ReadString(textRow))
>  {
>    text += textRow;
>  }
>
>Some lines in the textfile are quite long (200 chars) and some shorter.
>The first long line is read ok, but any long lines after that are truncated to 
>128 chars even if they are identical to the first one.
>
>Am I doing someting wrong or is it a bug?

It's a known bug in MFC 4.0; see the KB (Q152319).  I reviewed the MFC source
(VC++ 4.0) for the two ReadString() functions, and saw where the bug was coded
in ReadString(CString&).  (Basically, they used the allocation length rather
than the actual written length when deciding where to write the next 128-byte
chunk of the line being read; this would work if allocation were also done
in 128-byte chunks, but since it was done in 256-byte chunks instead, it left a 
128-byte stretch of garbage inside.)  However, I *also* saw that the other form
of the function, ReadString(LPSTR, UINT), did *not* have such a bug; in fact,
it merely calls fgets() on the underlying FILE object.  So, you can use that
form and thus sidestep the MFC 4.0 bug (which, BTW, was fixed in 4.1).

 CStdioFile MyFile;  // open it here or later
 CString MyString;
 const int MY_MAX_LEN = 1024;  // some number always big enough, but < 32K
 ...
 // this does *not* use the ReadString(CString&) with the VC++ 4.0 bug!
 MyFile.ReadString(MyString.GetBufferSetLength(MY_MAX_LEN), MY_MAX_LEN);
 // you might want to catch CMemoryException and CFileException here
 MyString.ReleaseBuffer();  // resizes to actual length
 ...

--Jeremy
-----From: "Peter R. Vermilye" 

----------
From: 	stefan.o@monitor.se[SMTP:stefan.o@monitor.se]
Sent: 	Friday, January 10, 1997 5:05 AM
To: 	mfc-l@netcom.com
Subject: 	Problems reading textfile with CStdioFile

Environment: NT 4.0 VC++ 4.0

I'm trying to read a textfile with code like this:
CString text;
CString textRow;
 CStdioFile file(filename, CFile::modeRead | CFile::typeText);=20
  while(file.ReadString(textRow))
  {
    text +=3D textRow;
  }

Some lines in the textfile are quite long (200 chars) and some shorter.
The first long line is read ok, but any long lines after that are =
truncated to=20
128 chars even
if they are identical to the first one.

Am I doing someting wrong or is it a bug?

/Stefan



I may be mistaken (so hopefully someone can correct me on this) but I =
think you need to allocate a buffer long enough to read in the longest =
line you anticipate.  I would probably modify your above code to read as =
follows:

CString text;			// to store the whole file
char* textRow =3D new char[4096];	// to read in a line at a time
CStdioFile file(filename, CFile::modeRead | CFile::typeText);
while (file.ReadString(textRow))
{
	text +=3D textRow;
}
delete [] textRow;

I'm not 100% sure of the above syntax, you get the drift.  While a =
CString doesn't add a lot of overhead to a program, it seems to me that =
for a simple buffer like you are proposing, a simple char[] would work =
just as well (if not better).  If you look through the MFC source, you =
will see similar instances where a char[] is used instead of a CString.

Hope this helps!!

Peter R. Vermilye
cpudude@mem.net

PS:  Who does Microsoft call for help?



-----From: Simon Coghlan 


Stefan and the mfc-l..

 I had this problem when I was writting a template interpretter for CGI =
to HTML data conversion under MSVC 4.1.. Its still there under 4.2b and =
will probably remain there..

It transpires that the Memory allocation routine that gets called as =
part of the ReadString method gets confused when the string is already =
greater than the first allocation block.. and it inadvertently 'forgets' =
what comes next.

Trace into the code after the first long string ( longer than 128 in =
your case ) and you will see it stop working..

Here is the fix..

In order to make the thing work.. force the CString varaible to be =
'Empty()' before each line read..


// 18/10/96		Sorted out the ReadString Bug
CString strDataLine;
while ( Template_File.ReadString( strDataLine ) )
{
	pThreadData->pTemplateHandler->m_pstrHtml_Page_Array->Add( strDataLine =
);
	// have to empty the string because it forgets to reallocate=20
	// the buffer in the ReadString Function
	strDataLine.Empty();
}

---------
Simon=20
----------<- Smurf-IV ->-------------
Tel : 0121 200 7713
Fax : +44 (0)121 212 0379
Email smurf-iv@sharelink.com
Member of the 'Team of CIS' ;-)
---------<- Fun To All ->------------
We are what we repeatedly do, Excellence, then, is not an act, but a =
habit.
Aristotle
The one who says it cannot be done, should never interrupt the one who =
is doing it.
The Roman Rule

On 10 January 1997 11:05, stefan.o@monitor.se wrote:
> Environment: NT 4.0 VC++ 4.0
>=20
> I'm trying to read a textfile with code like this:
> CString text;
> CString textRow;
>  CStdioFile file(filename, CFile::modeRead | CFile::typeText);=20
>   while(file.ReadString(textRow))
>   {
>     text +=3D textRow;
>   }
>=20
> Some lines in the textfile are quite long (200 chars) and some =
shorter.
> The first long line is read ok, but any long lines after that are =
truncated to=20
> 128 chars even
> if they are identical to the first one.
>=20
> Am I doing someting wrong or is it a bug?
>=20
> /Stefan
>=20
>=20
>=20





| Вернуться в корень Архива |