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

In summary, you can accumulate the values from each stream by reading them into a single variable, and then adding them together.
  • #1
burton95
54
0
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:
#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
 
Technology news on Phys.org
  • #2
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.
 
  • #3
An idiomatic way to approach this is
Code:
double d;
while(data_input >> d)
{
//add d to your sum
}

Here is another way which takes just one line
Code:
#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); 
}
 
  • Like
Likes 1 person
  • #4
And if you really do want to save all the numbers for later use:

Code:
#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.
 
  • Like
Likes 1 person
  • #5
Hey thanks. I got it with your help. Sorry it took so long to get back to you.
 

What is C++?

C++ is a general-purpose programming language that was developed as an extension of the C programming language. It is commonly used for developing high-performance applications such as operating systems, games, and graphical user interfaces.

How do I read-in numbers from an external file in C++?

You can use the ifstream class to read-in numbers from an external file in C++. First, you need to open the file using the open() function, then use the getline() function to read-in each line of the file. Finally, you can convert the string to a number using the stoi() function.

What if I don't know how many numbers are in the external file?

In C++, you can use a while loop to continuously read-in numbers from the external file until the end of the file is reached. You can use the eof() function to check if the end of the file has been reached.

How can I store the numbers from the external file in an array or vector?

You can use an int array or a vector to store the numbers from the external file. As you read-in each number, you can use the push_back() function to add it to the end of the vector, or you can use indexing to add it to a specific position in the array.

Are there any other methods for reading-in numbers from an external file in C++?

Yes, you can also use the fscanf() function or the get() function to read-in numbers from an external file in C++. However, using the ifstream class is the most commonly used method for reading-in numbers from an external file in C++.

Similar threads

  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
Back
Top