On 24 Jan 2006 15:30:28 -0800, nick <hachemn2@hotmail.com> wrote:
[color=blue]
> thanks for your reply :)
> i apologize for not sending my code, i should have included my code but
> i've been ading and deleting over and over till it became meaning less.
> so i have to start from the begining.
>
>
> int main(int argc, char *argv[])
> {
> string line;
>
> ifstream myfile ("c:\\documents and
> settings\\owner\\Desktop\\myfile.txt");
> if (myfile.is_open())
> {
> while (! myfile.eof ( ) )
> {
> getline (myfile,line);
> cout << line << endl;
>
> }
> myfile.close();
> }
>
> else cout << "Unable to open file"<<endl;
>
>
>
> system("PAUSE");
> return 0;
> }
>[/color]
Using !eof() like above is an error. See
http://www.gnomesane.net/code/doc/noteof/
Using getline as the loop condition means that if a line cannot be read,
the condition will fail (because getline returns the stream and the stream
would be in a not-good state). Checking eof() after the stream has failed
will tell you whether it failed due to eof; however, the only way getline
could fail is eof. (An out-of-memory condition with the default allocator
will throw an exception.)
#include <fstream>
#include <string>
#include <iostream>
int main( /*there's no reason to even have parameters if you don't use
them*/ ) {
using namespace std;
ifstream in ("c:/documents and settings/owner/Desktop/myfile.txt");
if ( !in ) {
cerr << "Unable to open file\n";
return 1;
}
for ( string line; getline( in, line ); ) {
cout << line << '\n';
// do something with line
}
// if you want the console to stay open after the program runs,
// run it from a console (e.g. run cmd and type the program's name)
return 0;
// no need for in.close(), ifstream will close itself when it destructs
}
--
Roger Pate <usenet2006@ml.qxxy.com>