parsing conf file in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 17
Reputation: invinate is an unknown quantity at this point 
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

parsing conf file in c++

 
0
  #1
Oct 8th, 2005
hi, i'm new here and got a really simple problem.
i need to parse a conf file, where # is a comment, like this:
  1. # Begin conf
  2. VAR1 = VAL1
  3. VAR2 = VAL2
  4. # end
i want to skip all comments and read VAL1 and VAL2 to members of a class that are std::string. This wouldn't be a problem for me in C, but i want to do it in C++ . Unfortunately, i'm not quite familiar with input and parsing C++ style. hope you can help...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,568
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: parsing conf file in c++

 
0
  #2
Oct 8th, 2005
>This wouldn't be a problem for me in C, but i want to do it in C++
You can use a C solution in C++, you know. But that aside, just read each line and don't do anything if the first non-whitespace character is '#':
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream in ( "somefile" );
  10.  
  11. if ( in.is_open() ) {
  12. string line;
  13.  
  14. while ( getline ( in, line ) ) {
  15. string::size_type i = line.find_first_not_of ( " \t\n\v" );
  16.  
  17. if ( i != string::npos && line[i] == '#' )
  18. continue;
  19.  
  20. // Process non-comment line
  21. }
  22. }
  23. }
This is much like you would do it in C:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ( void )
  5. {
  6. FILE *in = fopen ( "somefile", "r" );
  7.  
  8. if ( in != NULL ) {
  9. char line[1024];
  10.  
  11. while ( fgets ( line, sizeof line, in ) != NULL ) {
  12. size_t i = strspn ( line, " \t\n\v" );
  13.  
  14. if ( line[i] == '#' )
  15. continue;
  16.  
  17. /* Process non-comment line */
  18. }
  19. }
  20.  
  21. return 0;
  22. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 17
Reputation: invinate is an unknown quantity at this point 
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: parsing conf file in c++

 
0
  #3
Oct 8th, 2005
thank you, Narue, for replying.
yes, i think i understand how to use ifstream and how to read in a string now. But some things are still not clear.
In your code, what is string::npos ?
How to proccess the string? I'm not asking for code, just the idea.
In C, if i have a line like this: VAR1 = VAL1, with a pointer pointing to the beginning i would increment it till i find '=', then skip spaces and i would know that i have what i need. But in C++ using std::string how would i extract VAL1?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,568
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: parsing conf file in c++

 
0
  #4
Oct 8th, 2005
>In your code, what is string::npos?
In this context it means "not found".

>How to proccess the string?
I have no idea. That really depends on what you're going to do with the values in the file.

>But in C++ using std::string how would i extract VAL1?
Unless you feel like playing with iterators, or working with the find family of member functions for std::string, just use indices like you would an array of char and call it a day. If you're interested in the new ways, check www.dinkumware.com's reference section for further details on the std::string class.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 17
Reputation: invinate is an unknown quantity at this point 
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: parsing conf file in c++

 
0
  #5
Oct 8th, 2005
thanks again for responding and for the link. i'll do it the indeces way then.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 15
Reputation: Mareq is an unknown quantity at this point 
Solved Threads: 0
Mareq's Avatar
Mareq Mareq is offline Offline
Newbie Poster

Re: parsing conf file in c++

 
0
  #6
Nov 4th, 2005
And what about parsing XML configuration file? How can I parse XML - sequentially read through XML document and immediately after reading value of some XML tag assign this value into my variable? Which XML parser should I choose? (Actually, after reading some few tutorials I've already had chosen IBM's xml4c2, but is it good decision?) What exactly does it mean "SAX" and what is it good for?

Please help... Thx.



.mq.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: satarupa is an unknown quantity at this point 
Solved Threads: 0
satarupa satarupa is offline Offline
Newbie Poster

Re: parsing conf file in c++

 
0
  #7
Apr 16th, 2009
SAX parser can be used for xml file parsing. But it again depend on what kind of sorting you want. With SAX parser , file data can't be modified. And SAX parser always goes top to bottom. And it is fast compare to DOM parser. But if you want to modify xml data also, you need to use DOM parser. But it is slow, as this makes a database in heap and takes memory space more.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC