input file parsing in C++

  • Thread starter Gert Van den Eynde
  • Start date
G

Gert Van den Eynde

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
 
S

Sharad Kala

Gert Van den Eynde said:
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
 
R

Rolf Magnus

Gert said:
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.
 
T

Thomas Matthews

Gert said:
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
 
J

Jeff Flinn

Gert Van den Eynde said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top