User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,470 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,792 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 10748 | Replies: 5
Reply
Join Date: Oct 2005
Posts: 15
Reputation: invinate is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

parsing conf file in c++

  #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:
# Begin conf
VAR1 = VAL1
VAR2 = VAL2
# 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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: parsing conf file in c++

  #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 '#':
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  ifstream in ( "somefile" );

  if ( in.is_open() ) {
    string line;

    while ( getline ( in, line ) ) {
      string::size_type i = line.find_first_not_of ( " \t\n\v" );

      if ( i != string::npos && line[i] == '#' )
        continue;

      // Process non-comment line
    }
  }
}
This is much like you would do it in C:
#include <stdio.h>
#include <string.h>

int main ( void )
{
  FILE *in = fopen ( "somefile", "r" );

  if ( in != NULL ) {
    char line[1024];

    while ( fgets ( line, sizeof line, in ) != NULL ) {
      size_t i = strspn ( line, " \t\n\v" );

      if ( line[i] == '#' )
        continue;

      /* Process non-comment line */
    }
  }

  return 0;
}
I'm here to prove you wrong.
Reply With Quote  
Join Date: Oct 2005
Posts: 15
Reputation: invinate is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: parsing conf file in c++

  #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  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 488
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: parsing conf file in c++

  #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  
Join Date: Oct 2005
Posts: 15
Reputation: invinate is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: parsing conf file in c++

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

Solution Re: parsing conf file in c++

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC