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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:52:43+00:00 2026-05-13T19:52:43+00:00

Consider the following C++ program, which takes a file and prints each line. It’s

  • 0

Consider the following C++ program, which takes a file and prints each line. It’s a slice of a larger program where I later append to the file, based on what I see.

#include <fstream>
using std::fstream;
#include <iostream>
#include <string>
using std::string;

int main()
{
 fstream file("file.txt", fstream::in | fstream::out | fstream::app);

 string line;
 while (std::getline(file, line))
  std::cerr << line << std::endl;

 return 0;
}

Now apply this version of file.txt (One word on the first line, followed by a newline):

Rain

On my machine (Snow Leopard), this prints out nothing. On closer inspection, the first call to getline fails. Strangely, it also fails if I add a second line: still nothing is printed!

Can anyone solve this mystery?

  • 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-13T19:52:43+00:00Added an answer on May 13, 2026 at 7:52 pm

    When you say:

    fstream file("file.txt", fstream::in | fstream::out | fstream::app);
    

    you open the file in append mode – i.e. at the end. Just open it in read mode:

    fstream file("file.txt", fstream::in );
    

    or use an ifstream:

    ifstream file("file.txt" );
    

    And of course as Earwicker suggests, you should always test that the open succeeded.

    If you are determined to open in append mode, you can move the read pointer explicitly:

    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        fstream file( "afile.txt", ios::in | ios::out | ios::app );
        if ( ! file.is_open()  ) {
            cerr << "open failed" << endl;
            return 1;
        }
        else {
            file.seekg( 0, ios::beg );   // move read pointer
            string line;
            while( getline( file, line ) ) {
                cout << line << endl;
            }
        }
    }
    

    Edit: It seems that the combination of flags used in the opening of the file leads to implementation specific behaviour. The above code works with g++ on Windows, but not with g++ on Linux.

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

Sidebar

Related Questions

Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
Consider the following example: as part of configuration for my program the user supplies
I have a program which needs to turn many large one-dimensional numpy arrays of
Consider the following sequence of actual outcomes for a single static branch. T means
I have a custom UIView which is composed of many images, their positions are
What are some tips to reduce the memory usage of .NET applications? Consider the
In these days I'm playing with the C functions of atol(), atof() and atoi()
I couldn't find a suitable title for this. I'm going to express my query
I would like to compile fortran code on mac such that it does not

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.