![]() | |
| |||||||
| 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 |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Младенец Join Date: Oct 2001 Location: Atlanta, Ga
Posts: 1
Rep Power: 0 Reputation:
10 | 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? |
| | |
| | #2 |
| Administrator Join Date: Sep 2001 Location: South Korea, Gumi
Posts: 7,195
Blog Entries: 15 Rep Power: 10 Reputation:
313 | 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 |
| | |