473,385 Members | 1,730 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,385 software developers and data experts.

parsing files

Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
....
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.

cheers,
elcorto

Jan 17 '06 #1
10 2578
On 17 Jan 2006 12:41:29 -0800, "elcorto" <el*****@gmx.net> wrote:
Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.


Try this:
comp.text.xml

--
Bob Hairgrove
No**********@Home.com
Jan 17 '06 #2
I'm not planning to use XML.

cheers,
elcorto

Jan 17 '06 #3
elcorto wrote:
I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.


Read a line (using std::geline, for example) and search for the '=' sign,
and then split the line in two at the equal sign. Whatever is to the left
is your name. Whatever is to the right is the list of values. Split the
list of values at the commas and read out the values that way.

Is there a C++ language question here somewhere?

V
Jan 17 '06 #4
Isn't there an (fairly easy) way to do this in C? I thougt about
keeping my getopt mess, read the config file with Python and build the
cmdline options from there but that sound not very elegant to me ...

cheers,
steve

Jan 17 '06 #5
Thanx.

Is this no language question?
Sry if I'm posting to the wrong list. With wich list would I be better
off?

cheers

Jan 17 '06 #6
elcorto wrote:
Hi

I'm new to the list and I'm coming with a newbie question:

What would be the best way to parse simple (config) files like

config.txt:

keyA=value1, value2, value3, ...
keyB=value1, value2, value3, ...
...
(the value(i) should be converted to long or doulbe)

Up to now I used getopt (together with strtod() and so on for
converting the optarg) but as the input for my program gets more
complicated getopt
is starting to get painful.

Any hints are appreciated.

cheers,
elcorto


You could use an existing and tested parsing library such as

http://boost.org/doc/html/program_options.html

If you'd prefer to craft your own, you could make use of the tokenizer
library:

http://boost.org/libs/tokenizer/index.html

Cheers! --M

Jan 17 '06 #7
Ah, thx. I was looking for something like that.

cheers,
elcorto

Jan 17 '06 #8
elcorto wrote:
Is this no language question?
If you're asking what elements of the language to use, then I can tell you
to use 'std::ifstream', 'std::getline' and 'std::string' with all its
members ('find', 'substr', etc.) If you're asking "how to do it", then
you need to create an algorithm first, then translate it into C++. If you
have problems with the translation we can help, but you're the one who
needs to do most of the work.
Sry if I'm posting to the wrong list. With wich list would I be better
off?


I don't think there is one. You need to write a _program_ that does what
you want. I don't see any other way.

You could of course search the web for existing solutions. Visit
www.google.com and search for 'parsing strings'. Keywords like "name
value pairs" might help.

You could of course, use 'lex' and 'yacc'... They are both OT here, so we
can't help you either.

See, you have plenty of choices. Waiting for us to do your job is one of
them, but definitely not the best.

V
Jan 17 '06 #9
Did I say "send me your code"? My question was "what tools does C++
offer me, give me some hints, please".

Well now know that there is a string class which I didn't know before.
Thank you very much for answering my question.

cheers,
elcorto

Jan 17 '06 #10
elcorto wrote:
[..]
Well now know that there is a string class which I didn't know before.


Oh, I am sorry. For some strange reason I tend to assume that people
who come here for help at least know the basics of the language, like
the contents of the Standard Library, among other things. What book on
C++ are you reading that doesn't talk about 'std::string'?

V
Jan 17 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
4
by: Marian Jancar | last post by:
Hi, Is there a module for parsing spec files available? Marian -- -- Best Regards,
0
by: rick_muller | last post by:
I'm interested in parsing a (simple) Makefile using Python. I need to write a packager for a program I'm supporting, and would like to read the list of files in the makefile so that I only have to...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
3
by: Girish | last post by:
Hi All, I have written a component(ATL COM) that wraps Xerces C++ parser. I am firing necessary events for each of the notifications that I have handled for the Content and Error handler. The...
9
by: PedroX | last post by:
Hello: I need to parse some large XML files, and save the data in an Access DB. I was using MSXML 2 and ASP, but it turns out to be extremely slow when then XML documents are like 10 mb in...
35
by: .:mmac:. | last post by:
I have a bunch of files (Playlist files for media player) and I am trying to create an automatically generated web page that includes the last 20 or 30 of these files. The files are created every...
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...

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.