Search code examples
c++iofilestreamfstreamgetline

Following use of getline, cannot write to file


I'm building a large file I/O library and am currently struggling with the interoperability of getline() and writing to a file. My question below is alot like this one, which unfortunately remains unanswered: C++ After I use getline to read from a file I can no longer write to the txt file

Once I've used getline(), I'm no longer able to write to the file. Read requests with getline() will continue to operate, but write requests will fail. However, if I comment out the usage of getline(), the write operations will succeed.

My code is posted below. I've noticed that the failbit is being activated following the first write attempt. However, the reason for this occurring is unknown to me, as it is not active if I remove the getline() operations.

I should be clear -- I can read from the existing file (which contains TWO lines) perfectly. However, I am unable to write to that file, unless I remove the getline() statements.

Any help, is as always, appriciated.

// Includes
#include <fstream>
#include <iostream>
#include <string>

// Namespace
using namespace std;

int main(){
    // filestream object
    fstream workFile("testdoc.txt"); //fstream uses ios::in | ios::out by default
    // note that I have tried including ios::in | ios::out to no avail

    // read from file
    string grabbedLine;
    getline(workFile, grabbedLine);
    cout << "Line #1: " << grabbedLine << endl;

    // write to file
    workFile<< "Here is some output (#1)" << endl;

    // read from file
    getline(workFile, grabbedLine);
    cout << "Line #2: " << grabbedLine << endl;

    // write to file
    workFile<< "Here is some output (#2)" << endl;

    // wait for some input...
    getchar();
}

Current console output (as expected from text file):
Line #1: This is line#1
Line #2: This is line#2

Solution

  • Based on some experimentation, it looks like the << operator expects the offset to be at the end of the file. I can't explain it exactly, but it seems that the offset isn't where it expects it to be after you call getline(). I was able to get writing to the file to work by adding the line:

    workFile.seekg(ios_base::end);
    

    immediately before writing to the file. This however, will put the offset at the end of the file and not correctly read the second line afterward.

    What I think you want to do is open two file handles, one that you will read from and one that you will write to:

    int main(){
        // filestream object
        fstream workFileRead("testdoc.txt", ios_base::in);
        fstream workFileWrite("testdoc.txt" , ios_base::app | ios_base::out);
    
        // read from file
        string grabbedLine;
        getline(workFileRead, grabbedLine);
        cout << "Line #1: " << grabbedLine << endl;
    
        // write to file
        workFileWrite << "Here is some output (#1)" << endl;
    
        // read from file
        getline(workFileRead, grabbedLine);
        cout << "Line #2: " << grabbedLine << endl;
    
        // write to file
        workFileWrite<< "Here is some output (#2)" << endl;
    
        // wait for some input...
        getchar();
    }