Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read entire file with CStdioFile::ReadString 2

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hello,
How do I know when eof has been reached? I have tried
Code:
while(myFile.ReadString(strTmp))
{
   //blah
}
but it does not seem to work. Actually, I wouldn't really expect it to work anyway. The docs say ReadString will return false if EOF was reached WITHOUT reading ANY data.

Any ideas?


many thanks,

Barry
 
Barry,

The behavior I see is that the first ReadString at EOF returns TRUE with an empty string. Then the next one returns FALSE. Does that help?

-pete
 
Ah yes! That's good!

This little bit of code works:
Code:
CString strLine;
while (true)
{                
    if ((!file.ReadString(strLine)) && (strLine.IsEmpty()))
    {
        break;
    } 
}



Thanks for the help!


Barry
 
With a minor adjustment you can avoid that "funny" looking while(true).

Code:
while (file.ReadString(strLine) && !strLine.IsEmpty())

Though it will stop if you read any empty line from the file, which perhaps isn't the intent...

Perhaps it's something like this you're after:
Code:
while (file.ReadString(strLine))
{
  if(!strLine.IsEmpty())
  {
    .
    .
  }
}



/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Yes, the second one is what I need. I find it a bit strange that there seems to be no simple way to use ReadString() and test for the EOF all on the same line. (Like your first example)
Normally in Java, I do something like
Code:
while((strTmp = reader.readLine()) != null)
{
   //blah
}


thanks,

Barry
 
with using Win32API you can do like this:
HFILE hfFile = 0;
OFSTRUCT of;
DWORD ftr = GetFileAttributes(&quot;x:\path...\somefile&quot;);
if(ftr == -1)
{
return 0;
}
hfFile = OpenFile(&quot;x:\path...\somefile&quot;, &of, OF_READ | OF_SHARE_DENY_NONE);
unsigned long sz = 0;
unsigned long hsz = 0;
unsigned long ret_sz = 0;
if(0xFFFFFFFF == (sz = GetFileSize((void*)hfFile, &hsz)))
{
MessageBox(0, &quot;function GetFileSize(...)
falied&quot;, &quot;server debug message&quot;, 0);
}
query = new char[sz + 1];
query[sz] = 0;
ReadFile((void*)hfFile, query, sz, &ret_sz, 0);
CloseHandle((void*)hfFile);

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Btw, in what way doesn't just the
Code:
while(myFile.ReadString(strTmp))
work?

MSDN:
[Returns] FALSE if end-of-file was reached without reading any data.

I interptret this that as long as there is a string to read (empty or not) it will return TRUE, but when after last line is read it will return FALSE.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
yes, it works. But there could be some situations when my method is better. But in the most cases your method is very good.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top