user680288
user680288

Reputation: 49

C Parser for parsing and reading Config file with defined format?

I need to parse a configuration file , I need to compare the string variable and get the values.

The variable and values are seperated by white spaces, For example, TestscenConf.config file contains the following,

SCENARIO_TYPE    WIRELESS_TEST
SIMULATION_TIME  200
[1] [IP_ADDR1][1] 192.168.1.0
[2] [IP_ADDR2][1] 192.168.1.2
[2] [IP_ADDR2][2] 192.168.1.3

Here, everything is seperated by space.

I want to compare variable name and get its value.

How can I parse it in C or C++.

Thank you,

Arun

Upvotes: 0

Views: 1186

Answers (2)

Oleg Trakhman
Oleg Trakhman

Reputation: 2082

Of course you can use one of this *scanf() functions, but i strongly suggest you to use common open standart for your configs (such as XML or JSON). it's much better because of many reasons:

1) You get stable working parser for any OS and any programming language. Just take a look at JSON: http://www.json.org/ - how many languages are supported!

2) What happens if you add one another item in you config? or item of other format (list of ip-addresses, for example)? Take universal, ubiquitous solution!

Upvotes: 2

pmg
pmg

Reputation: 108978

fopen, (fgets, sscanf, and strcmp) in a loop, and fclose come to mind.

Upvotes: 0

Related Questions