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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:57:23+00:00 2026-05-26T01:57:23+00:00

I have a text file that contains the following data. The first line is

  • 0

I have a text file that contains the following data.

The first line is this:

5 4 3 2 1

The second line is this:

1 2 3 4 5

I am trying to read data from one line at a time because my first linked-list object is going to use the data from the first line and my second linked-list object is going to use data from the second line. The best I have been able to come up with is the following function:

void polynomial::allocate_poly(std::ifstream& in, const char* file, const char* number)
{

    in.open(file);

    std::string str;
    char b;
    int m = 0;

    for(int i = 0; !in.eof(); ++i)
    {
        in >> b;
        m = b - '0';
        a.insert(m);
    }

There is a few problems with this approach. I have tried different binary operators in my for loop such as b == '\n' and none of them seem to trigger when b is a newline character.

Also allocating the numbers from the file this way it looks like
5 5 4 3 2 1 1 2 3 4 5 , so it seems to be copying an extra 5 somewhere, I am not sure if this is the eof bit or not.

I have also attempted to use the getline function but for some reason it seems to only copy the first integer and then dumps the rest of the file. I know certainly I am not using it correctly but all the examples I can find are for typing the file name such as cin.getline and I want to be able to pass my file name as a command line argument when running the program instead.

My question is how can I allocate the numbers on the first row up to the newline char and then pass the ifstream in variable to another object to allocate the second line? Thanks for your help.

  • 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-26T01:57:23+00:00Added an answer on May 26, 2026 at 1:57 am

    You don’t say what a is, but never mind: If you want line based parsing, you need to have getline in the outer loop. Also, never use eof, as it doesn’t do what you want. Rather use the implicit conversion to bool to check if an operation succeeded.

    Here’s the typical gig:

    #include <sstream>
    #include <fstream>
    #include <string>
    
    std::string line;
    std::ifstream infile("myfile.txt");
    
    while (std::getline(infile, line))  // this does the checking!
    {
      std::istringstream iss(line);
      char c;
    
      while (iss >> c)
      {
        int value = c - '0';
        // process value
      }
    }
    

    However, this conversion from char to int is cumbersome and fragile. Why not read an integer directly?

      int value;
      while (iss >> value) { /* ... */ }
    

    Edit: Based on your comment, here’s the code to read exactly two lines:

    std::string line;
    int val;
    
    if (std::getline(infile, line))
    {
      std::istringstream iss(line);
      while (iss >> value) { /* process first line */ }
    }
    if (std::getline(infile, line))
    {
      std::istringstream iss(line);
      while (iss >> value) { /* process second line */ }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that contains a data dump from a database. This
I have Text file that contains data separated with a comma , . How
I have a text file that contains a long list of entries (one on
I have a text file that contains a data like ID Name Path IsTrue
I have a tab-delimited text file that I am parsing. Its first column contains
I have to write a program that read from a file that contains the
I have a text file that contains this text: What's New in this Version
Hi I have a text file which contains some numerical data. Of that text
I have a text file that contains meta-urls in the following form: http://www.xyz.com/.*services/ http://www.xyz.com/.*/wireless
I have a text file that contains localized language strings that is currently encoded

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.