Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters

LinkBack Thread Tools
Old 03.10.2001, 02:08   #1
Join Date: 10 2001
Location: Atlanta, Ga
Posts: 1
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Question basic fstream question

I'm in a second year C++ class and using MS Visual C++ as my compiler. My question is- when reading a file with ifstream, and using a loop to count words and characters what should I use to end the loop? I have it set like this, each character of the input file is being put into a temp variable, ch, and counted. <BR>do{<BR> mycode<BR>}while(ch != 'EOF') This isn't working (of course) So what is the correct syntax to end a loop at the end of a file?
Reply With Quote
Old 03.10.2001, 02:37   #2
acid's Avatar
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Blog Entries: 16
Downloads: 102
Uploads: 34
Reputation: 561 | 6

There is an article in MSDN about it. Here is example from that article for you:

<BR>#include <fstream.h>

void main()<BR>{<BR> char ch;<BR> ifstream tfile( "payroll", ios::binary | ios::nocreate );<BR> if( tfile ) {<BR> while ( tfile.good() ) {<BR> streampos here = tfile.tellg();<BR> tfile.get( ch );<BR> if ( ch == ' ' )<BR> cout << "\nPosition " << here << " is a space";<BR> }<BR> }<BR> else {<BR> cout << "ERROR: Cannot open file 'payroll'." << endl;<BR> }<BR>}<BR>

as you can see above basic_ios::good() member function has been used to determine whether EOF condition occured or not.

Hope it helps<BR>acid
Reply With Quote

Thread Tools

На правах рекламы:

All times are GMT. The time now is 15:59.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.