• 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
https://www.neowin.net/forum/topic/860548-c-file-operations-word-search-etc/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Well, first of all you'd need to use the stdio.h header and youd use the fscanf fucntion, here's a good reference to it: http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/

This is assuming you already have your file Handler created.

  • 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?

  • 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.

  • 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.

  • 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
  • 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 :)

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

    • No registered users viewing this page.
  • Posts

    • You pay just $100 per TB with this rare 4TB PCIe Gen4 NVMe SSD deal by Sayan Sen SSDs and GPUs are incredibly hard to get nowadays due to high pricing. Discounts are quite rare which is why we report on them as soon as we spot a good deal. For example AMD's new 9070 GRE was finally up for sale at a very good price of just $500 thanks to a special coupon. Sadly that deal is gone but if you happen to be looking for a 4TB NVMe SSD and can spend around $400 there is a really good offer on sale that you should not miss out on as TeamGroup's 4TB G50 model is on sale for that that price which means you are only paying $100 per TB, a very good deal in the current market (purchase link under the specs table down below). The TeamGroup T-FORCE G50 NVMe SSD is a PCIe Gen4 drive and as such it promises to deliver sequential read speeds of up to 5,000 MB/s, helping accelerate game loading, file transfers, and everyday computing tasks. Since this is a 4TB drive you can use it for a gaming library to take advantage of things like DirectStorage. The SSD features an InnoGrit controller and SLC caching technology to support consistent performance. An ultra-thin, patented graphene heatsink is included to aid in heat dissipation. Get it at the link below: Team Group T-FORCE G50 4TB Internal SSD (TM8FFE004T0C129): $449.99 + $50 off w/ promo code SSF69668, limited offer => $39.99 (Sold and Shipped by Newegg US) Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • I agree. I also think Phil stayed too long. They should definitely fire whoever thought all a console platform needed was Call of Duty, Elder Scrolls, and Fallout to survive. Asha and crew are still saying they need more Elder Scrolls and Fallout games. They simply don't get it.
    • Macbook Air is an appealing option, as are plethora of Windows devices with various different CPU's
  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Steven P.
      89
    5. 5
      Nick H.
      71
  • Tell a friend

    Love Neowin? Tell a friend!