Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

How do you parsing a text file in c/c++

This topic is 4768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

any one give me a clue on how to parse a text file in c/c++ I can load the file fine now I just need to look for some symbols and words and then load what is after those symbols and words into a verible. I know how to load text file and how to set a string or char from the text file to a verible. But I cant choose were to start loading. Can any one help with this? some simple code would help or some tutorials to read Thanks

Share this post


Link to post
Share on other sites
Advertisement
Parsing/lexing a file in a given language is a non-trivial task that keeps many very smart people very busy with research papers. Unfortunately, this also means that a single post is beyond the scope of the information that would need to be conveyed. But if you're interested in learning more about the topic, there are a lot of terrific books out there. One that comes highly recommended is the so-called Dragon Book, which gives a terrific introduction into the theory of parsing, compilers, lexers, and the like.

Share this post


Link to post
Share on other sites
Please go to here: http://www.gamedev.net/community/forums/topic.asp?topic_id=341903
We have some good codes in here, you can use and enjoy ;)

Bye

Share this post


Link to post
Share on other sites
Okay, let me partially retract my previous statement. Were you talking about parsing a C++ file (i.e., some source code), or were you talking about merely reading in some kind of simple file from input (e.g., a comma-separated list of latitude/longitude positions) and doing things with it?

If it's the latter, then a Google search on "C++ fstream file input tutorial" and similar terms will probably give you what you want. See here for a good starting point.

Share this post


Link to post
Share on other sites
It was the last one. There is no way with my skill I would be bale to make a compiler or that type of parser that would be way out of my leag.


Thanks that will get me started

thanks for all the help.I will probubly have more questions later.

Share this post


Link to post
Share on other sites
Depending how complex the data to parse, you may want to use Flex and Bison, with them you create parser code (in the form of a c or c++ file you just compile in the usual way) that decomposes the data into usable tokens.

It would be overkill to use them for parsing comma separated values, but I've succesfuly used them to create a .MAP parser and a Doom 3 MD5 model loader, just read the manuals, they're not complicated at all.

Share this post


Link to post
Share on other sites
A very, very good reference on this for beginner/intermediate skilled programmer is 'Exploring Programming and Computer Science with C++' by Owen Astrachan of Duke University. In Section 9.3 he presents 'Case Study, Removing Comments with State Machines'.

If you don't have access to this book, you can look through some of the material at http://www.cs.duke.edu/~ola/book.html and you can also download his sample code, which is quite instructive on its own. This file you want to study is 'decomment.cc'.

--random_thinker

Share this post


Link to post
Share on other sites
If the data is simple (ie: The format is deterministic) writing a parser is a matter of fstreaming and interpretting the data. If its more free form but relatively simple then rolling your own is an option, google for tokenization, parser, lexical annalysis, etc. If its something like a simple computer language or complex free-form data you might want to check out flex, bison, yacc, etc. I've also heard good things about boost::spirit.

Share this post


Link to post
Share on other sites
Or, depending on the data you're storing, you might want to check out TinyXML or a similar XML library.

XML is a really cool and flexible way to store all kinds of data.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
any one give me a clue on how to parse a text file in c/c++

I can load the file fine now I just need to look for some symbols and words and then load what is after those symbols and words into a verible.

I know how to load text file and how to set a string or char from the text file to a verible. But I cant choose were to start loading.

Can any one help with this?

some simple code would help or some tutorials to read

Thanks


use CRegExp library. 25kB sources

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!