AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Languages, Compilers and Interpreters C,C++,C#,.NET,Java,PHP,Perl,SQL and more

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Oct 3, 2001, 02:08   #1
Младенец
 
Join Date: Oct 2001
Location: Atlanta, Ga
Posts: 1
Rep Power: 0
Reputation: 10
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?
fallenzeo is offline   Reply With Quote Quote selected
Old Oct 3, 2001, 02:37   #2
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: South Korea, Gumi
Posts: 7,195
Blog Entries: 15
Rep Power: 10
Reputation: 313
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
__________________
Chat with acid


acid is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:02.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link