help in reading a text file

Posted on 2006-06-12
Last Modified: 2010-04-15

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


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
Question by:aja101498
  • 2
LVL 53

Expert Comment

ID: 16885749
Check out fscanf :

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.
LVL 24

Accepted Solution

fridom earned 125 total points
ID: 16885927
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);
  } 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);
  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

LVL 15

Expert Comment

ID: 16907445
Ah - didn't realize it was homework. I'll watch out for that in the future!
LVL 15

Expert Comment

ID: 17061621
Not fair - you deleted my response!

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question