Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++; How to read-in a unknown amount of numbers from an external file

  1. Jul 9, 2014 #1
    Basically I want to take an unknown amount of variables and sum them up. I'm sure it's simple. I know there is some way to tell VS to keep reading the opened stream from my numbers.txt file.

    Code (Text):

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

    ifstream data_input;
    data_input.open("numbers.txt");
    .....


     
    Before I just knew the number of variables and would declare them and read them in seperatley and sum them up. How do I say "When no more int's are left in the "numbers.txt" file quite reading and then sum them up??

    Anthony
     
  2. jcsd
  3. Jul 9, 2014 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Read the numbers one at a time in a loop. Terminate the loop when there is an error reading the next number. (Hitting the end of the file will cause an error, but you might have other errors like invalid input in the file.)

    You don't need to read all the numbers first and then add them all up. You can add each number as you read it.

    If you really want to store all the numbers, use a class like vector, where you don't have to declare the number of variables at the start of the program.
     
  4. Jul 9, 2014 #3
    An idiomatic way to approach this is
    Code (Text):

    double d;
    while(data_input >> d)
    {
    //add d to your sum
    }
     
    Here is another way which takes just one line
    Code (Text):

    #include <fstream>
    #include <iterator>
    #include <numeric>

    int main()
    {
        using namespace std;
        ifstream data_input("numbers.txt");
        double s = accumulate(istream_iterator<double>(data_input),
                                    istream_iterator<double>(), 0.0);
    }
     
     
  5. Jul 9, 2014 #4

    jtbell

    User Avatar

    Staff: Mentor

    And if you really do want to save all the numbers for later use:

    Code (Text):

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

    int main ()
    {
        ifstream data_input;
        data_input.open("numbers.txt");

        double d;
        vector<double> v;  // size is zero initially

        while (data_input >> d)
        {
            // append to the end of the vector,
            // increasing its size by one
            v.push_back(d);
        }

        // verify that we read the data we wanted
        for (int k = 0; k < v.size(); ++k)
        {
            cout << v[k] << " ";
        }
        cout << endl;

        return 0;
    }
     
    I'll leave it to you to write the code to calculate the sum.
     
  6. Jul 20, 2014 #5
    Hey thanks. I got it with your help. Sorry it took so long to get back to you.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook