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

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


Problem reading a file

Wu Bin -- whelan@radiusplc.co.uk
Wednesday, March 05, 1997

Environment: VC++ 4.1, Windows 95, Windows NT 3.51

Hi,

I had a problem when reading a file using:

CStdioFile inFile;
CString strLine;
.......
while (inFile.ReadString(strLine)!=NULL)
{
  .......
}

It works OK under Windows95. But, under Windows NT 3.51 it
reads the first record of the file (the first text line separated
by a carrige return), but from the second record on it just reads the 
first 128 characters, truncating the rest of the line.
 
Every text line of the file is 228 characters long.
Please can you help.

Thanks in advance,

Bin Wu
Radius Computer Services,





Jeremy H. Griffith -- jeremy@omsys.com
Thursday, March 06, 1997

[Mini-digest: 3 responses]

On 5 Mar 97 15:28:02 +0000, "Wu Bin"  wrote:

>Environment: VC++ 4.1, Windows 95, Windows NT 3.51
>I had a problem when reading a file using:
>
>CStdioFile inFile;
>CString strLine;
>.......
>while (inFile.ReadString(strLine)!=NULL)
>{
>  .......
>}
>
>It works OK under Windows95. But, under Windows NT 3.51 it
>reads the first record of the file (the first text line separated
>by a carrige return), but from the second record on it just reads the 
>first 128 characters, truncating the rest of the line.
> 
>Every text line of the file is 228 characters long.

This problem with CStdioFile::ReadString(CString&) is documented as KB bug
report Q152319.  I have reviewed the MFC source (in VC++ 4.0) for the two
ReadString() functions, and seen where the bug is coded in FILETXT.CPP, in
ReadString(CString&). (Basically, they used the allocation length rather
than the actual written string length to determine where to write next,
leaving a hole after the first 128 characters.)  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,
this should work for you:

 CStdioFile inFile;
 CString strLine;
 const int MY_MAX_LEN = 250;  // some number always big enough, but < 32K
 .......
 // this does *not* use the ReadString(CString&) with the VC++ 4.0 bug!
 while (inFile.ReadString(strLine.GetBufferSetLength(MY_MAX_LEN), MY_MAX_LEN))
 {
   strLine.ReleaseBuffer();  // resizes to actual length
  .......
 }
 // you might want to catch CMemoryException and CFileException here too...

Also, according to the bug report, this was an MFC 4.0 bug *fixed* in 4.1.
Indeed, looking at the 4.1 source, I noted the fix was made.  Is it possible
that you are still linking to MFC 4.0 libraries when you build for NT?  Or
is this a new bug?  In either case, using the code above should step around
it successfully.

--Jeremy
-----From: coghlans 

I Sent this a while back but here it is again..

 I had this problem when I was writing a template interpreter for CGI to =
HTML data conversion under MSVC 4.1..  with Win 95 and NT 4.0

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 variable 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 [nee CIS now] FOA in CPA' ;-)
---------<- 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


-----Original Message-----
From:	Wu Bin [SMTP:whelan@radiusplc.co.uk]
Sent:	Wednesday, March 05, 1997 3:28 PM
To:	mfc-l@netcom.com
Subject:	Problem reading a file

Environment: VC++ 4.1, Windows 95, Windows NT 3.51

Hi,

I had a problem when reading a file using:

CStdioFile inFile;
CString strLine;
......
while (inFile.ReadString(strLine)!=3DNULL)
{
  .......
}

It works OK under Windows95. But, under Windows NT 3.51 it
reads the first record of the file (the first text line separated
by a carrige return), but from the second record on it just reads the=20
first 128 characters, truncating the rest of the line.
=20
Every text line of the file is 228 characters long.
Please can you help.

Thanks in advance,

Bin Wu
Radius Computer Services,


-----From: Roma 

Wu Bin wrote:
> 
> Environment: VC++ 4.1, Windows 95, Windows NT 3.51
> 
> Hi,
> 
> I had a problem when reading a file using:
> 
> CStdioFile inFile;
> CString strLine;
> .......
> while (inFile.ReadString(strLine)!=NULL)
> {
>   .......
> }
> 
> It works OK under Windows95. But, under Windows NT 3.51 it
> reads the first record of the file (the first text line separated
> by a carrige return), but from the second record on it just reads the
> first 128 characters, truncating the rest of the line.
> 
> Every text line of the file is 228 characters long.
> Please can you help.
> 
> Thanks in advance,
> 
> Bin Wu
> Radius Computer Services,

Hello, 
I remember I've got the same problem and I was forced to get rid of 
ReadString(CString&). ReadString(LPCSTR buf, int bufSize) works fine.

This is a beginning of the ReadString(CString&) source code from my VC
4.0:
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;
	[snip]
}
As far you can see this 128 number there I guess the error is somewhere
in the code
of this function - but I don't know where. They don't read-in just 128
byte, of course,
they do re-allocation of the CString's buffer... I think you can go with
debugger inside
this ReadString annd see what's happening, if you are intersted in using
_this_ function.

HTH, Roma.



Mike Blaszczak -- mikeblas@nwlink.com
Thursday, March 06, 1997

At 15:28 3/5/97 +0000, you wrote:
>Environment: VC++ 4.1, Windows 95, Windows NT 3.51

>I had a problem when reading a file using: [CFile::ReadString]

>It works OK under Windows95. But, under Windows NT 3.51 it
>reads the first record of the file (the first text line separated
>by a carrige return),

It's hard for me to believe that it's not failing in both operating
systems.  The same code should be executed in both operating systems.
Nothing that's happening is OS-dependent.

>but from the second record on it just reads the 
>first 128 characters, truncating the rest of the line.
>Every text line of the file is 228 characters long.

The problem you've described is a bug that was fixed.  MFC 4.2-flat
and MFC 4.2b both have the fix.


.B ekiM
http://www.nwlink.com/~mikeblas/
These words are my own. I do not speak on behalf of Microsoft.
           This performance was not lip-synched.





Become an MFC-L member | Вернуться в корень Архива |