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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:19:37+00:00 2026-05-28T08:19:37+00:00

I am reading a text file line by line in C++. I’m using this

  • 0

I am reading a text file line by line in C++. I’m using this code:

while (inFile)
{
getline(inFile,oneLine);
}

This is a text file:

-------

This file is a test to see 
how we can reverse the words
on one line.

Let's see how it works.

Here's a long one with a quote from The Autumn of the Patriarch. Let's see if I can say it all in one breath and if your program can read it all at once:
Another line at the end just to test.
-------

The problem is I can read only the paragraph starts with “Here’s a long etc…” and it stops “at once:”
I couldn’t solve to read all text. Do you have any suggestion?

  • 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-28T08:19:38+00:00Added an answer on May 28, 2026 at 8:19 am

    The correct line reading idiom is:

    std::ifstream infile("thefile.txt");
    
    for (std::string line; std::getline(infile, line); )
    {
        // process "line"
    }
    

    Or the alternative for people who don’t like for loops:

    {
        std::string line;
        while (std::getline(infile, line))
        {
            // process "line"
        }
    }
    

    Note that this works as expected even if the file couldn’t be opened, though you might want to add an additional check if (infile) at the top if you want to produce a dedicated diagnostic for that condition.

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

Sidebar

Related Questions

I'm reading lines from a text file using (line-seq (reader input.txt)) . This collection
When reading lines from a text file using python, the end-line character often needs
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I'm reading text from a flat file in c# and need to test whether
I'm reading lines off of text file and I'm wondering if this is a
Here I'm reading text file which contains a integer in each line and I'm
I am reading from a text file line by line. StreamReader reader = new
I am reading a text file which I know its 38th line is Uncalibrated
I am currently reading in a text file in VB.Net using Dim fileReader As
I'm having problems reading lines from a text file in PHP. I have this

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.