469,644 Members | 1,966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

input file parsing in C++

Hi all,

Could you give me some pointers on how to parse a text input file in C++?
Most will be config-file style input (keyword = data), but some maybe
'structures' like material{ name = n, position = x,y,z}.

Things that I have in my mind now are: 1) simply reading in strings,
analysing the strings myself, 2) writing a lexer/parser, 3) xml parsing, 4)
line by line and RegExp.

1) may be too much work and too much re-work when the input file syntax
changes
2) may be too much work to begin with, no experience there. Any good
tutorials?
3) no experience there. Any good tutorials?
4) is there a good regexp library for c++?

thanks for any tips,

gert
Jul 22 '05 #1
4 3230
Gert Van den Eynde wrote:
Hi all,

Could you give me some pointers on how to parse a text input file in
C++? Most will be config-file style input (keyword = data), but some
maybe 'structures' like material{ name = n, position = x,y,z}.

Things that I have in my mind now are: 1) simply reading in strings,
analysing the strings myself, 2) writing a lexer/parser, 3) xml
parsing, 4) line by line and RegExp.

1) may be too much work and too much re-work when the input file
syntax changes
Does it change so often?
2) may be too much work to begin with, no experience there. Any good
tutorials?
You could use a lexer/parser generator like flex and yacc, which does a
lot of the dirty work for you. It gets pretty easy then if you know a
bit about regular expressions and bnf.
3) no experience there. Any good tutorials?
There are quite a lot of xml DOM and SAX parsers out there, and most of
them have good documentation.
4) is there a good regexp library for c++?


libpcre and the boost regular expressions come to mind.

Jul 22 '05 #2

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:ca**********@ikaria.belnet.be...
Hi all,

Could you give me some pointers on how to parse a text input file in C++?
Most will be config-file style input (keyword = data), but some maybe
'structures' like material{ name = n, position = x,y,z}.


Here is some demo code -

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
ifstream file;
string fileName ="file.txt";
file.open(fileName.c_str());
if (!file){
cout << "Error in openening file";
return EXIT_FAILURE;
}
string lineread;
while(std::getline(file, lineread)) // Read line by line
{
//...lineread contains the line read
// Process the string now.
}
}

A good book to read is "The C++ Standard Library" by Nicolai M. Josuttis.
Otherwise you can get Bruce Eckel's "Thinking in C" online.
-Sharad
Jul 22 '05 #3
Gert Van den Eynde wrote:
Hi all,

Could you give me some pointers on how to parse a text input file in C++?
Most will be config-file style input (keyword = data), but some maybe
'structures' like material{ name = n, position = x,y,z}.

Things that I have in my mind now are: 1) simply reading in strings,
analysing the strings myself, 2) writing a lexer/parser, 3) xml parsing, 4)
line by line and RegExp.

1) may be too much work and too much re-work when the input file syntax
changes
2) may be too much work to begin with, no experience there. Any good
tutorials?
3) no experience there. Any good tutorials?
4) is there a good regexp library for c++?

thanks for any tips,

gert


I suggest writing it yourself. A lexer, xml parsing and regexp are too
much overhead. I believe that a simple std::map of
<string, function_pointer> would suffice. Or you could use an array
of <string, function_pointer> records. If the string matches the
entry, then execute the function pointer _associated_with_ the string.

Let the function take care of evaluating the objects. Just pass the
string to the the function.

First write a small simple program that reads in a line and extracts
the keyword and displays it. Next add code to lookup the keyword and
execute a null (stubbed) function. At this point the main code should
not need much changing. Most of the changes will take place in the
table and the functions to process the keyword.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:ca**********@ikaria.belnet.be...
Hi all,

Could you give me some pointers on how to parse a text input file in C++?
Most will be config-file style input (keyword = data), but some maybe
'structures' like material{ name = n, position = x,y,z}.
See http://www.boost.org/libs/spirit/index.html. This allows you to write
true C++ expressions that define the parser rules. It's very quick and easy
to learn(IMHO).

For example, to parse the above:
rule<> rKeyWord = lexeme_d[ ( ( alpha_p | '_' ) >> *( alnum_p |
'_' ) ) ];
rule<> rData = ( rKeyWord | int_p ); // plus some handling for
list type
rule<> rAssignment = rKeyWord >> '=' rData;
rule<> rStruct = rKeyWord >> '{' >> ( rAssignment % ',' ) >> '}';

rule<> rInput = *( rAssignment | rStruct );

parse_info<> lResults = parse( "value = 123\n material{ name = somename,
val = 456 }"
, rInput // the rule above
, space_p // skip white space
);

There are facilities to generate an parse/abstract syntax tree, or to
associate directives that assign the parsed data to your variables.


Things that I have in my mind now are: 1) simply reading in strings,
analysing the strings myself, 2) writing a lexer/parser, 3) xml parsing, 4) line by line and RegExp.

1) may be too much work and too much re-work when the input file syntax
changes
2) may be too much work to begin with, no experience there. Any good
tutorials?
3) no experience there. Any good tutorials?
4) is there a good regexp library for c++?


boost::spirit is the hands down best way to go here, IMHO of course. There
are numerous examples and excellent documentation to get you started.

Jeff F
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by championsleeper | last post: by
37 posts views Thread by Jason Heyes | last post: by
2 posts views Thread by SophistiCat | last post: by
3 posts views Thread by Fao, Sean | last post: by
14 posts views Thread by n3o | last post: by
3 posts views Thread by sab | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.