473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3488
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: championsleeper | last post by:
we have a system that recives plain text files from numerous external sources (database and others). our system recieves the information and then processes it. each line in the input file is a...
0
by: Thomas Engelmeier | last post by:
Hi, I need an expat-alike parser (ABI-wise as much a drop-in replacement as possible) for C/C++ with the following additions: - in order to handle some tags with nesting in completely...
37
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function...
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
3
by: Fao, Sean | last post by:
Hello all, As stated in another message, it's been a long time since I've done any C coding and I'm not feeling comfortable that I'm doing this correctly. Basically, I'd like to verify that my...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
2
by: Andy | last post by:
Hi guys, I'm writing a program with a feature of accepting user input as command text and parsing it to correct function calls...example: "5 minutes later"/"5 min later"/"5 minute...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
3
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.