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
AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
6K
Replies
13
Views
2K
Replies
25
Views
2K
Replies
6
Views
2K
Replies
5
Views
3K
Replies
10
Views
2K
Replies
15
Views
3K
Replies
3
Views
2K
Back
Top