C++ Help (fstream)

Hello, I have a couple questions about using fstream. I am curious on how I would do something like :
To begin with there is a .txt file with a set of numbers in them.
- How would I be able to read how many numbers there are in this .txt file and then take those numbers and add them?

Any help would be really great. I have been stumped with this for a couple days now and cant find the correct way to do it.

Thanks,
Torm04
How would I be able to read how many numbers there are in this .txt file and then take those numbers and add them?
Why in this order? Why not get them all and count them at the same time?
It could be in that order, butttttt... not exactly sure how to do that either..
In your numbers are integers:
1
2
3
4
5
6
7
8
9
10
11
12
std::ifstream in("Some_file.txt");
std::vector<int> values;
int temp;
while(in >> temp)
    values.push_back(temp);
std::cout  << values.size();//Outputs amount of numbers
//values contains all numbers from a file.

//Alternatively:
std::ifstream in("Some_file.txt");
std::vector<int> values(std::istream_iterator<int>(in), std::istream_iterator<int>());
std::cout  << values.size();
What do all of the std:: 's mean? Sorry, I haven't yet leaned things like std:: or whatever the vector thing is o-o
I drew up something earlier and it is not working how I would like it to..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main() {
	ifstream inputFile;
	int num, amount;
	
	inputFile.open("numbers.txt");
	cout << "Reading data from the file" << endl;
	
	while (inputFile >> num) //Displays all of the numbers in the file.
	{
		amount++; //Makes amount = to how many numbers there are.
		cout << num << endl;
		
	}
	cout << "There are " << amount << " numbers in this file." << endl;
	//This is definately not working how I want it to.
	
	inputFile.close();
	return 0;
	
}
amount++; What is the amount value in the beginning?

or whatever the vector thing is
vector is the standard C++ container used to stre multimple values of same type
http://www.mochima.com/tutorials/vectors.html

What do all of the std:: 's mean?
std is a namespace where all standard library resides. It is better to use explicitely qualified names (std::cin) than dumping whole std namespace into global (using namespace std): name clashing and hard-to-notice bugs can follow.
Last edited on
Thank you for pointing this out.. that is what was wrong.. I need to crack down on this.. lol I make this mistake often.
It's just like when you use
using namespace std;
but calling them one by one(see namespace in the reference section).
First of all, since you are dealing with a bunch of numbers, that means you need to have something between them right? (In the code above, inputFile>>num stops when they see /n, spaces,/t etc.(see ref.)).
Well for the last(above) post you've made, you need to specify the first value of
amount
. say int num = 0, amount = 0;
My advice is that you work on the basics first(say using a for loop instead of while) and start building up until you reach more advance topics(like vectors). Hopefully this helps. :)
Last edited on
That was helpful, thank you.
Alright. So basically, I am supposed to be writing a program that calculates things like the number of numbers in the file (Done with this part)
-Gets the sum of the numbers in the file. (No idea how to do this ;_;)
-ect ect..
Basically.. Im not sure how to read out the numbers and then add up those numbers or multiply them to get an average.

Also.. a bit stumped on how to make the program know the highest number on here..


Scratch all of that.. figured it out. Now I am stuck on one thing. How do I find the lowest and highest number in the .txt file and output it?

Thanks,
Torm04
Last edited on
you can try saving them to a numerical variable(most of the cases would be int), or array , or vector etc.. and loop for the biggest number, say:

int biggest = 0;
int smallest = 0;
for(unsigned int i = 0;i<vectorsize;i++)
{
if(vector[i] > biggest)
{biggest = vector[i];}
if(vector[i]< smallest)
{smallest = vector[i];}
}

NOTE: this is just a pseudo-code, variables etc. are meant for examples.
YOu can do as rjvc said, or you can modify your solution:
1
2
3
4
5
6
7
8
9
int biggest = <some really small number>;
int snallest = <some really big number>;
while (inputFile >> num) {
    amount++;
    if(num > biggest) biggest = num;
    if(nume < smallest) smallest = num;
    cout << num << endl;
}
//output buggest and smallest 
Thank you everyone that helped me with this. I got the assignment finished. Thanks so much guys.
Topic archived. No new replies allowed.