Solved

help in reading a text file

Posted on 2006-06-12
7
533 Views
Last Modified: 2010-04-15
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
Comment
Question by:aja101498
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 16885749
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
 
LVL 24

Accepted Solution

by:
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);
    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
 
LVL 15

Expert Comment

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

Expert Comment

by:bpmurray
ID: 17061621
Not fair - you deleted my response!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

733 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