• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

ifstream not reading all the way towards end of file c++

Hello i'm testing out a basic code that just reads numbers from a file and adds them up but it is not reading beyound the 3rd int and i don't know why..

for example i type in
10
20
30
40
into my nums.txt file and then it only reads 10,20,30 and adds them up but does not read the 40.. not sure what is the issue


#include <iostream>
#include<fstream>
using namespace std;
int SumFileInts(ifstream &read);

int main(void)
{

	ifstream in_stream;

	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		cout <<"input file opening failed.";
    }
SumFileInts(in_stream);
cout <<SumFileInts(in_stream);

}

int SumFileInts(ifstream &read)
{
	int numbers;
	int total;
	int sum;
	total = numbers;
	
if(read >> numbers)
{
     total =numbers + numbers;
	 sum = total+numbers;
	 return sum;
}
 
 read.close();
}

Open in new window

0
businessesatoz
Asked:
businessesatoz
  • 3
  • 2
1 Solution
 
jkrCommented:
Your reading control flow is a bit flawed - that should be more like
#include <iostream>
#include<fstream>
using namespace std;
int SumFileInts(ifstream &read);

int main(void)
{

	ifstream in_stream;

	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		cout <<"input file opening failed.";
    }


  cout <<SumFileInts(in_stream);

}

int SumFileInts(ifstream &read)
{
	int number;
	int total = 0;

	
  while(!read.eof())
  {
     read >> number;
     total += number;
     number = 0;
  }
 
  read.close();

  return total;
}

Open in new window

0
 
sarabandeCommented:
there is no loop in SumFileInts. so you have only

total =numbers + numbers;
sum = total+numbers;

Open in new window


which is 3 times of the number of first line.

instead, you should replace if (read >> number) by while (read >> number) such that the read was continued while the read does not fail. in the loop simply add the number to sum or total but not multiple times.

Sara
0
 
businessesatozAuthor Commented:
thanks .
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sarabandeCommented:
the while(!read.eof()) would be an infinite loop when the nums.txt contains some non-numeric value. in that case the read fails but the eof flag never was reached. so better use while(read) or while (read >> number) which would test on both eof and fail.

Sara
0
 
jkrCommented:
???
0
 
sarabandeCommented:
i tested with

while(!read.eof())
{   
    read >> numbers;
    sum += numbers;
}

Open in new window


and input file

Student: Sara
Grade: C
Average: 82

Open in new window


and it runs infinite as expected.

businessesatoz, when doing the test i found that you call SumFileInts twice in your main. look into the code jkr has posted, where the correct code was implemented.

Sara
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now