read 2 lines of my infile and average values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dispatch4599
    New Member
    • Mar 2008
    • 1

    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?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by dispatch4599
    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

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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

      Working...