The following file prints the second line of a file:
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
// Open your file
ifstream someStream( "textFile.txt" );
// Set up a place to store our data read from the file
string line;
// Read and throw away the first line simply by doing
// nothing with it and reading again
getline( someStream, line );
// Now begin your useful code
while( !someStream.eof() ) {
// This will just over write the first line read
getline( someStream, line );
cout << line << endl;
}
return 0;
}
I would like to ask how can i write to that file, I am asking because if i use
ofstream
instead of ifstream
I can't use getline
function and if i use ofstream
i can't write to that file.
The error message I get if I use ifstream
and try to write to that file :
IntelliSense: no operator "<<" matches these operands operand types are: std::ifstream << int