Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3930760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:17:15+00:00 2026-05-19T23:17:15+00:00

I’m building a large file I/O library and am currently struggling with the interoperability

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:17:16+00:00Added an answer on May 19, 2026 at 11:17 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.