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

Started by
14 comments, last by MaulingMonkey 18 years, 7 months ago
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
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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
OpenGl + C++
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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.
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.
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
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
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.

throw table_exception("(? ???)? ? ???");

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

This topic is closed to new replies.

Advertisement