0

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

  • Do it in two separate steps, one reading all the input with an `ifstream` and after closing open another `ofstream` for writing to this file. – πάντα ῥεῖ Jun 02 '14 at 14:30
  • Related, checking for `eof` inside a loop is most likely not what you wanted ([Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/q/5605125)). You should rather put the whole `getline` call as the while condition. – user1942027 Jun 02 '14 at 14:34

1 Answers1

2

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

std::getline is made for std::istream and it's speclializations (You will not be able to use getline for writing stuff).

To write to the file:

ifstream someStream( "textFile.txt" );
// your code here (I won't repeat it)

someStream.close(); // flush and close the stream
ofstream output("textFile.txt", std::ios::ate|std::ios::app); // append at end of file
output << "this string is appended at end of the file";
std::string interestingData{ "this is not a fish" };
output << interestingData; // place interesting data in the file

A few other things you should keep in mind while working with i/o streams:

// 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 );

This is no good: If the file doesn't exist or doesn't contain the correct data type, or you have no read permissions and so on, getline will set someStream into an invalid state (different than "eof" that basically means "getting input failed"), it will not fill in the line, and your code will not know that.

The correct code to read a file, line by line can be found here.

To reset the file before writing into it, ensure you use the correct state flags when opening the file:

ofstream someStream( "textFile.txt", std::ios::trunc );
someStream.close(); // flush and reset the file, with no content added (= make empty)
Community
  • 1
  • 1
utnapistim
  • 24,817
  • 3
  • 41
  • 76
  • I want before writing to file , The file should be made empty. Please tell how to do this, –  Jun 02 '14 at 14:40
  • To clean the file, you open the file for writing, without append flag, then close it (I will edit the post). – utnapistim Jun 02 '14 at 14:43