PDA

View Full Version : Read .ini files



paolom
9th November 2009, 14:10
Hi...

Is there with Qt something to read easily text file with Windows .ini style ?!?

Like this:



[GROUP 1]

key1 = value1
key2 = value2

[GROUP 2]

key1 = value1
....



Thanks

ftrastour
9th November 2009, 15:00
Hi,

You can use QSettings :
QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )

Fred.

mgoetz
9th November 2009, 15:37
Please not that QSettings only properly reads INI files that were also created with QSettings. You might be lucky to read other INI files too, but it is not guaranteed.

paolom
9th November 2009, 15:46
Ok...but If I want to parse with Qt an existing INI file like this:



[TOP_LEFT]
[ELEMENT1]
comment = Name
tag = Cliente_Name
sequence = 1
Modality = all
[ELEMENT2]
comment = Birth_Date
tag = Client_Birth_Date
sequence = 2
Modality = all
[TOP_CENTER]
[ELEMENT 1]
comment = example
tag = example
sequence = 1
Modality = all
[ELEMENT 2]
comment = example2
tag = example2
sequence = 2
Modality = all
[TOP]
value = prove


I've try with QSettings and childGroups() but it doesn't works...

Which is the better way to do this?

Thanks

wysota
9th November 2009, 15:51
As far as I know this is not a proper "ini" file. For the above mentioned file I'd use regexps.

paolom
9th November 2009, 15:56
Sorry...but with RegExp how can you pass the text file?!

FinderCheng
9th November 2009, 16:01
If you have to use this format of ini file, why not change it to XML file?

wysota
9th November 2009, 17:18
Sorry...but with RegExp how can you pass the text file?!
You parse it line by line and decide what to do with the result afterwards. For instance if you know the line is like "\[([A-Za-z0-9_]+)\]" then you know it is a (sub)group so you can create a group node in your final structure. If the line is like "([A-Za-z0-9_]+) = (.*)" then you know it is an item and you can create an item node in your final structure.