Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 537
  • Last Modified:

help in reading a text file

Hi

I am new to C and I have the following data file. I need a simple C program to read this data file. the program should skip the hearders ( first 4 lines) and read the average only.
Can somebody help. Thanks

 Aja

Yearly rain
agent: a300
area 12as
======
date    Average
Jan     1.345
Feb     1.566
Mar     0.700
Apr     1.200
May     1.000
Jun     2.000
Jul     1.820
Aug    1.530
Sep    2.121
Oct     2.433
Nov    2.777
Dec    3.000
0
aja101498
Asked:
aja101498
  • 2
1 Solution
 
Infinity08Commented:
Check out fscanf :

http://www.cplusplus.com/ref/cstdio/fscanf.html

and especially the format types you can use with it. There's also an example at the bottom of that page.

To skip certain parts, look at the * modifier.
0
 
fridomCommented:
I'm not sure if this is a homework question, I assume it is not. So
here are my takes on it.
Write function to skip reading over lines till some limit, as I see the lines to ignore are here 5 not 4.

Reading a line from a file then can be done with a combination from fgets and sscanf.

putting it together (without any error handling)
and without the latter function (IMHO you should try first)
#include <stdio.h>

enum {SOME_LIMIT=512};

void skip_lines (FILE *fp, int lines_to_skip) {
  char dummy[SOME_LIMIT];
  char *pc = NULL;
  int i = lines_to_skip;
  do {
    pc = fgets(dummy, SOME_LIMIT, fp);
    i--;
  } while (i > 0 && pc);
}



int main(void){
  char *file_name = "t1.txt";
  FILE *fp = NULL;
  int i_rval = 0;
  int i = 1;
  double avg;

  fp = fopen(file_name, "r");
  if (fp){
    skip_lines(fp, 5);
   
    while ((i_rval = read_next_average(fp, &avg)) > 0) {
      printf("avg on line %d (+ 5) = %g\n", i, avg);
      ++i;
    }
    fclose(fp);
  }
 
  return 0;
 
}

I got the following output:
avg on line 1 (+ 5) = 1.345
avg on line 2 (+ 5) = 1.566
avg on line 3 (+ 5) = 0.7
avg on line 4 (+ 5) = 1.2
avg on line 5 (+ 5) = 1
avg on line 6 (+ 5) = 2
avg on line 7 (+ 5) = 1.82
avg on line 8 (+ 5) = 1.53
avg on line 9 (+ 5) = 2.121
avg on line 10 (+ 5) = 2.433
avg on line 11 (+ 5) = 2.777
avg on line 12 (+ 5) = 3


That looks quite reasonable to me.

So now you just have to implement the function read_next_average.
It's prototyp is
int read_next_average (FILE *fp, double *avg);
As you see return value = 0 implies some sort of error.

So go for it

Friedrich
0
 
bpmurrayCommented:
Ah - didn't realize it was homework. I'll watch out for that in the future!
0
 
bpmurrayCommented:
Not fair - you deleted my response!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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