Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help in reading a text file

Posted on 2006-06-12
7
Medium Priority
?
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

610 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