473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

read 2 lines of my infile and average values

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?
Mar 7 '08 #1
2 1663
gpraghuram
1,275 Expert 1GB
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?

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
Mar 7 '08 #2
Ganon11
3,652 Expert 2GB
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.
Mar 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Martin Hvidberg | last post by:
Dear group I need to make a very simple piece of code in C, that can be command line executed and will compile on Linux, i.e. gcc. It should read a ascii Comma Separated Values (CSV) file and...
2
by: mojju | last post by:
Perl Experts: open(INFILE, $filename) or die("Cannot read input file: $!"); #How can I use this while loop to scan/read numbers from three lines and #find the smallest number ? while (...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
24
by: Ground21 | last post by:
Hello. How could I read the whole text file line after line from the end of file? (I want to ~copy~ file) I want to copy file in this way: file 1: a b
15
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
8
by: Jromero | last post by:
My code Another class = new Student; /* public static void copyLines() throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; ...
1
by: Alenik1989 | last post by:
hi again i have this code int main() { ifstream infile; ofstream outfile; float num1,num2,num3,limit1=0,limit2=0,counter1=0,counter2=0,sum=0; float aver; infile.open("testin.txt");
10
by: vincex200 | last post by:
My group needs help with this program. We attempted to start it and got no where. Please help us. Write a C++ program that will read data from a file, perform computation on the data, then print...
13
by: youngjin.michael | last post by:
Hello, I am new to Python and have one simple question to which I cannot find a satisfactory solution. I want to read text line-by-line from a text file, but want to ignore only the first line....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.