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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:00:03+00:00 2026-05-31T23:00:03+00:00

I am trying to input data from a text file in C++. The text

  • 0

I am trying to input data from a text file in C++.
The text file is in that format:

4 15
3 516
25 52 etc.

Each line contains two integers. I don’t know the number of lines in the file so I can bind enough memory and this is what I have come into as a way to solve that:

ifstream filein;
filein.open("text.txt",ios::in);
int count=0;

while (!filein.eof())
    {
        count++;
        filein>>temporary;
    }
count=count/2; // This is the number of lines in the text file.

My problem is that I can’t figure out a way to reset

filein

into the initial state (to the beggining of the file so I can actually input the data) other than closing the input stream and opening it again. Is there any other way to do that?

  • 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-31T23:00:05+00:00Added an answer on May 31, 2026 at 11:00 pm

    Rather than answer the question you asked, I’m going to answer the question you didn’t ask, namely:

    Q: How can I read in all the lines of the file if I don’t know how many lines there are?

    A: Use a std::vector<>.

    If you want to read in all of the numbers, regardless of pairing:

    // all code fragments untested. typos are possible
    int i;
    std::vector<int> all_of_the_values;
    while(filein >> i)
        all_of_the_values.push_back(i);
    

    If you want to read in all of the numbers, putting alternating numbers into different data structures:

    int i, j;
    std::vector<int> first_values;
    std::vector<int> second_values;
    while(filein >> i >> j) {
        first_values.push_back(i);
        second_values.push_back(j);
    

    If you want to read in all of the numbers, storing them in some sort of data structure:

    int i, j;
    struct S {int i; int j;};
    std::vector<S> values;
    while(filein >> i >> j) {
        S s = {i, j};
        values.push_back(s);
    }
    

    Finally, if you want to read the file a line at a time, keeping the first two numbers from each line, discarding the remainder of each line, and storing them a user-defined data structure:

    std::vector<MyClass> v;
    std::string sline;
    while(std::getline(filein, sline)) {
      std::istringstream isline(sline);
      int i, j;
      if(isline >> i >> j) {
        values.push_back(MyClass(i, j));
      }
    }
    

    Aside: never use eof() or good() in a loop conditional. Doing so almost always produces buggy code, as it would have in your case. Instead prefer invoking the input function in the condition, as I have done above.

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

Sidebar

Related Questions

I'm trying to read from a text file to input data to my java
I am trying to display some data from text file with an array. The
I am trying to load some data from a text file into a vector
I am trying to write data from form inputs to a text file using
I'm trying to input data from an XML file in a C++ program using
I'm trying to reset some input text fields, inside of a data table, using
Hi there I'm trying to input 3 integers from the keyboard and print rows
I’m trying to validate input by using egrep and regex.Here is the line from
Trying to simulate a HTTP POST that needs to combine some INPUT/TEXT fields along
My Perl program takes some text from a disk file as input, wraps it

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.