I'm only a beginner in C++ and can not figure out how to read only 2 lines of my infile. The lines consist of all integers. Objective is to add all the integers in just those two lines and average them. Can someone give me some direction?
read 2 lines of my infile and average values
Collapse
X
-
Tags: None
-
Originally posted by dispatch4599I'm only a beginner in C++ and can not figure out how to read only 2 lines of my infile. The lines consist of all integers. Objective is to add all the integers in just those two lines and average them. Can someone give me some direction?
Open the file
use fgets to read the first line
use fgets to read the second line
close the file
After reading the line use sscanf to get the integer values and sum it up.
Hope this helps
Raghuram -
Of course, that would be a great idea if it was C. But this is C++, so we should use C++ functions and classes.
Try opening the file with an ifstream, using getline(infile, string) twice (to get two lines), and then try out a stringstream to put in each line and get out integers.Comment
Comment