A cin function for files?

Hello, I have this piece of code here, which opens a text file and puts text in it.

1
2
3
     std::ofstream myfile;
     myfile.open(filename);
     myfile << text << endl;


I was wondering if there is a >> operator to get lines from the text file.
For example:

1
2
3
     std::ofstream myfile;
     myfile.open(filename);
     myfile >> text >> endl;
Last edited on
Yep, there is: the std::ifstream class (with >> operator): http://www.cplusplus.com/reference/fstream/ifstream/ and
http://www.cplusplus.com/doc/tutorial/files/
Thanks Fransje.

Just a few other questions, how do you know when you are at the end of the file?
For example:

1
2
3
4
5
6
while(!FALSE)
{
     myfile >> text;
     if (text==NULL)
     {break;}
}


Also, would I define text as string? Does >> take the full line or the full file or a char?

Thanks!
Last edited on
Just a few other questions, how do you know when you are at the end of the file?


Generally you would structure your while loop like so:

1
2
3
4
5
6
while ( myfile >> text )
{
    // process
}

// we're done with myfile. 
Topic archived. No new replies allowed.