• 0

[C] File operations, word search etc


Question

I am working in C on a text file that contains METAR data.

I would like to count how many entries file contains and still I don't know where to start and how to go through.

Counting lines is out of option, as METAR files may contain some additional 'commented' header data, so my first thought was to count all "METAR"s as in every entry there is just one. And still there is a problem of this commented header - who knows if someone didn't put some "METAR" in there?

Still, I do not know how, C is a new area for me.

There is a link to METAR on wikipedia: METAR

and some of my sample file:

##########################################################
# Query made at 11/09/2009 14:49:47 UTC
# Time interval: from 11/01/2009 14:00  to 11/09/2009 14:00  UTC
##########################################################

#
#*Some commented stuff*
#

###################################
#  METAR/SPECI from EPGD
###################################
200911091400 METAR EPGD 091400Z 09009KT 0800 R29/1600 -RA FG VV003 05/05
						Q1011=
200911091330 METAR EPGD 091330Z 10012KT 1000 R29/1800 -RA BR VV002 05/05
						Q1011=
200911091300 METAR EPGD 091300Z 10012KT 1600 -RA BR OVC001 05/04 Q1011=
200911091230 METAR EPGD 091230Z 09011KT 1600 -RA BR BKN001 05/04 Q1011=
200911091200 METAR EPGD 091200Z 09012KT 1500 -RA BR BKN001 05/04 Q1011=
200911091130 METAR EPGD 091130Z 09012KT 2000 -RA BR BKN001 05/04 Q1012=
200911091100 METAR EPGD 091100Z 09013KT 2000 -DZ BR BKN001 05/04 Q1012=
200911091030 METAR EPGD 091030Z 09013KT 1200 -RADZ BR SCT001 BKN002 05/04
						Q1012=

And as you can see there might be some problems

Maybe counting "=" is a bright idea?

Do you have any ideas?

Any help is appreciated.

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Good idea. I am a C-newbie - can someone show me the way/link/example?

I know how to put every line into a separate variable, but how to check the first sign of each line/variable?

Link to comment
Share on other sites

  • 0

Changed my mind.

The code that have isn't good for file reading.

But I have gone through string search so part of work is done ;)

I hope this is my last question in this case :what is the best way to insert data to array line-by-line?

Link to comment
Share on other sites

  • 0
Changed my mind.

The code that have isn't good for file reading.

But I have gone through string search so part of work is done ;)

I hope this is my last question in this case :what is the best way to insert data to array line-by-line?

Linked list or hashtable with fgets.

Link to comment
Share on other sites

  • 0

Just figured it out by myself.

I knew fgets, but I didnt know how to get the next line - I feel like an idiot know, it was that simple, it goes forward by itself with next call... :)

Link to comment
Share on other sites

  • 0
Just figured it out by myself.

I knew fgets, but I didnt know how to get the next line - I feel like an idiot know, it was that simple, it goes forward by itself with next call... :)

It'll only go to the next line if you read enough characters to get to the '\n' at the end of the line, which you're right, it does in normal operation. All depends on how many characters you want to read.

Link to comment
Share on other sites

  • 0

Now I would like to read a wind strength from each record.

200911091200 METAR EPGD 091200Z 09012KT 1500 -RA BR BKN001 05/04 Q1011=

This is in bold. First 3 digits are the wind direction, next 2 is the strenght, KT is shortcut for knots.

I guess that it is possible to search each record for "KT" (function returning position in strong - there is sth like that?) and read 2 places before.

This is my theory, I am now looking for proper functions. Is this good? Please, advice :)

Edited by Karwowski
Link to comment
Share on other sites

  • 0

Maybe not the best way - i treat string as an array and I check each index if it equals K, if the next equals T, two previous indexes must point the strenght of wind. :)

Maybe not the best, but it works :)

Thanks for all replies :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.