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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:06:46+00:00 2026-06-14T12:06:46+00:00

2012/11/13 Update: I found my question had already been asked. Here is a good

  • 0

2012/11/13 Update:
I found my question had already been asked.
Here is a good solution for handling different line ending text files:
Getting std :: ifstream to handle LF, CR, and CRLF?

Is it possible to contribute to libstdc++? How?


2012/11/11

I found there’s something wrong with cout.
If there are two strings returned from getline(),
the second string will overwrite the first one in the output.

This is sample code:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    //normal code
    cout << "Normal result:" << endl;
    string str1 = "hello";
    cout << str1;
    str1 = "123";
    cout << str1;

    cout << endl;

    //broken code
    cout << "Bug?" << endl;
    ifstream fin;
    fin.open("test.dat");

    string str;

    getline(fin, str);
    cout << str;

    getline(fin, str);
    cout << str;

    fin.close();
    return 0;
}

And here is the input file (test.dat):

hello
123

The output will be:

Normal result:
hello123
Bug?
123lo

I’m using ubuntu 12.10 64-bit,
and the version of compiler is
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2.
Any advice?
Is there anyone tell me where to file a bug?

  • 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-14T12:06:47+00:00Added an answer on June 14, 2026 at 12:06 pm

    More likely than a bug in libstdc++ (which can occur, as well as in gcc, but are rather rare these days), there is incorrect line termination in your input file – likely it is using the DOS/Windows CR+LF line endings, which – as getline() discards the LF – results in the second string written over the first one. You can easily see this, if you run output of your program through some kind of hex-dumper, e.g. xxd.

    Either check for \r at the end of the strings you read (btw, MacOS to version 9 used just this as the EOL marker), fix your input, or duly add new-lines to output, when printing.

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

Sidebar

Related Questions

Platform: ASP.NET 4.0, MVC 4 RC, VS 2012 Update: I've answer my question, myself.
Edit (2012-04-12): Since this question was asked it is now possible (as of jQuery
Big Update : As I finally found the real solution, I also discovered the
Maybe this question has been anwsered, maybe not. I want to update an object
SOLUTION My working solution can be found in the answer or in my update
Update 2012-02-11 : Since my question is quite old but very popular, I'd propose
It's probably an already asked question, but I just don't know what are the
I am using Visual studio 2011 beta with the april 2012 update installed .I
How to update Active directory from Ax 2012 ? for example, how to update
I have this query update user_remember_me set when='2012-07-06 05:44:27', hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7' where user = '21';

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.