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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:38:32+00:00 2026-05-29T03:38:32+00:00

I’m trying to read a text file one line at a time and print

  • 0

I’m trying to read a text file one line at a time and print each line to a terminal window. I’m compiling on a mac using g++, e.g., g++ cpp3.cpp -o cpp3.

The text file looks like this:

20100000001 20100000001.xml
20100000002 20100000002.xml
20100000003 20100000003.xml
20100000004 20100000004.xml

Here is my code:

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

using namespace std;

int main()
{
    ifstream ifs("file.txt");
    string line;
    while(getline(ifs,line)) {
        getline(ifs,line);
        cout << "[ " << line << " ]" << endl;
    }
return 0;
}

I’d like the output to look like:

[20100000001    20100000001.xml]
[20100000002    20100000002.xml]
[20100000003    20100000003.xml]
[20100000004    20100000004.xml]

However, everything is printed to a single line in the terminal. In other words, each line from the text file is written on top of the last. Furthermore, the brackets, “[” and “]” appear to only be written once, so that it basically prints “[“, then the entire contents of the file, and then “]”.

So this is what the final output to the terminal looks like:

user$ ./cpp3
 ]10000000401   20100000004.xml

user$

Can anyone explain why this is happening and/or how to fix the problem?

  • 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-29T03:38:33+00:00Added an answer on May 29, 2026 at 3:38 am

    Take a look at the data file – perhaps with od -c.

    Everything written on top of each other line sounds as if you’re printing \r (carriage return) and not \n (newline) in the output, maybe because the data does not contain newlines. Maybe the file comes from a Windows environment, and the getline() is stripping the newline, leaving carriage return at the end, and the output is not reinstating the newline?

    Or, maybe the file only contains \r line endings – in which case, the getline() would read the entire file as a single line, and output it as a single line. You could demonstrate that by adding a counter to the output, incrementing it for each output line. Your observation about ‘square brackets printed once’ would suggest this might be better than the stricken suggestion. You have an endl in the output, which is important.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to render a haml file in a javascript response like so:
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.