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 8568239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:04:07+00:00 2026-06-11T18:04:07+00:00

While debugging someone else’s code I came across an interaction between C++’s fstream object,

  • 0

While debugging someone else’s code I came across an interaction between C++’s fstream object, input via the stream operator and ios::app which I was not previously aware of.

Assume file.txt exists and contains textual data. The fstream in its original context was long lived and served for both input and output. The following code does not work as expected (provides no output from the file), error handling code has been omitted:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream f("file.txt", ios::app);
    string in;
    f >> in;
    cout << in << endl;
    f.close();
    return 0;
}

Changing the file open statement to the following resolves the issue:

fstream f("file.txt");

Is this expected behaviour? Should it not be possible to open an fstream object with ios::app and expect input via the stream operators to behave correctly?

Compiled with g++ 4.6.3 on 64-bit Linux and mingw-g++ 4.4.1 on 32-bit Windows 7

  • 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-06-11T18:04:08+00:00Added an answer on June 11, 2026 at 6:04 pm

    If you check the std::fstream constructor you will note that the default argument is a bitfield of the flags ios_base::in and ios_base::out. By supplying only ios_base::app as the flag you make the file append-only. If you want both input and append, then you have to use e.g. ios::in | ios::app.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging javascript written by someone else, I came across some code that I've
While debugging into some Oracle code, I came across this query: SELECT TPM_TASK.TASKID FROM
I'm debugging someone else's code, and I've run into a situation I wouldn't know
I am debugging code someone else wrote that calls a lot of stored procedures
I'm new to using logcat and debugging in Android. I've analyzing someone else's code
I'm encountering something a bit bizarre, but maybe someone else came across this before.
While debugging and keep pressing F5, if the source code does not exist, eclipse
i get this dialog while debugging code in visual studio 2008. (your step-into request
While debugging the code in Eclipse, I runned into the following problem. There is
While debugging Java code that uses reflection in Eclipse, some times I need to

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.