Solved

help in reading a text file

Posted on 2006-06-12
7
529 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now