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

Reply
 
Thread Tools

basic fstream question
Old 03.10.2001, 02:08   #1
Младенец
 
Join Date: 10 2001
Location: Atlanta, Ga
Posts: 1
Rep Power: 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?

Old 03.10.2001, 02:37   #2
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Blog Entries: 16
Rep Power: 7
Post

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




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 20:22.
Top

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