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

  • Thread starter Thread starter burton95
  • Start date Start date
  • Tags Tags
    File Numbers
Click For Summary
To sum an unknown number of integers from a file in C++, read the numbers one at a time in a loop until the end of the file is reached. Use an ifstream to open the file and read each number, adding it to a sum as you go. If you want to store the numbers for later use, utilize a vector to dynamically hold the values. An efficient approach to sum the numbers directly is to use the `accumulate` function with iterators, allowing for a concise one-liner solution. The discussion emphasizes that there’s no need to predefine the number of variables, making the code flexible and efficient for handling varying input sizes.
burton95
Messages
54
Reaction score
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
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.
 
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
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
Hey thanks. I got it with your help. Sorry it took so long to get back to you.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K