• 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

    • I don’t get why if I bought the game on the last get I needed to pay $40 to upgrade to the new version in the first place and people who love the game and play a lot would have upgraded already so this is just PR and a chance to grab new players who forgot the game long time ago I think
    • The term "use" is doing a lot of heavy lifting in that headline. "Use" can mean opening ChatGPT occasionally to ask for the definition of a word or information about a specific topic. If you frame the question around how many people use it as a daily driver in their work or personal lives, that number is a lot smaller. Those are the people who pay for AI. Nearly everyone else is happy to use it for free, but doesn't see enough value in it to pay for it.
    • No support for Windows Hello!
    • I think you meant the "ntfs3" driver, but yes there have been a lot of fixes for it in this release and previous releases, not 100% sure if the issue you mentioned is fixed though. In any case, the new "ntfs" driver in 7.1 doesn't have that issue (at least, no reports of such have come thru), but your kernel needs to explicitly enable support for the new driver first (like how CachyOS kernel has it), and you need to edit your mount points in /etc/fstab to use "ntfs" instead of the other drivers.
    • Epic Games says Unreal Engine 6 will help developers "build content faster" using AI models by Pulasthi Ariyasinghe Epic Games is rolling out the latest major update to Unreal Engine 5 today, and at the same time, the company also dropped some information on the next-generation version of the product, Unreal Engine 6. This was already revealed a few weeks ago alongside the new Rocket League upgrade reveal. The company says it is combining the features of Unreal Engine and Unreal Editor for Fortnite to create this new version of its popular media creation tool. On top of creating entire games, the new engine will also focus on letting developers operate large-scale live service titles more easily, whether by shipping content into their own ecosystems or into Fortnite. The use of large language models is also mentioned here, with Epic saying it will be a core part of the engine. "We see LLMs, generative AI models, and tools like Claude and Codex playing a central role in helping you build content faster while maintaining the creative control you need," adds the company. Here is the rundown of what's new about version 6 of Unreal Engine: With all these changes to the programming model, portability upgrades, and generative AI integration, Epic says the new version of the engine will "change a lot about how games are made." The company aims to ship Unreal Engine 6 into early access in late 2027, with a full release planned for 12-18 months later. Epic Games also dropped a lengthy blog post about the new Unreal Engine 5.8 update for game developers over here. The release is focused on delivering better performance, customization, and streamlined workflows for development teams. This will be the final major update for this version of the engine before Epic switches to focus fully on Unreal Engine 6's early access launch.
  • Recent Achievements

    • One Month Later
      Vincian earned a badge
      One Month Later
    • First Post
      Jocimo earned a badge
      First Post
    • Week One Done
      suprememobiles48 earned a badge
      Week One Done
    • One Month Later
      Windows Guy earned a badge
      One Month Later
    • One Month Later
      Prasann earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      502
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      86
    4. 4
      Steven P.
      67
    5. 5
      neufuse
      65
  • Tell a friend

    Love Neowin? Tell a friend!