Kohinoor24
September 19th, 2002, 03:22 AM
I want to read an ini file which is as follows:
I need a function which takes the ini-File name & the section to read as parameters to this function.
and I want to read each string(Full string) in the section one by one.
I want no windows specific code as I want the code to be compiled under Linux as well.
;32 Bit - 7.0 Line
[builder]
version=3
menus=11
bindings=2
bellOnError=1
bellOnWarning=1
[Options]
VirtualDesktop=0
AlwaysOnTop=0
Could any one help me on this regard.
Waldo2k2
September 19th, 2002, 11:09 AM
As far as getting the file name...are you searching for it? or do you have it already? I'm not exactly sure how to search for files in a non windows fashion...you can do system calls, but i'm not sure what the unix equivilant to the DOS FIND command is. As far as reading in the parameters, that's easier.
#include <iostream>
#include <fstream>
#include <string>
char * buffer=new char[256];//multiples work best, 8,16,32,64,128,etc.
char * parameters=new char[128];
char * builderParameters=new char[128];
char * options=new char[128];
ifstream fin("file.ini");//open reading of 'file.ini'
int main()
{
//sorry about not tabbing this, but i don't feel like hitting space five times every line
fin>>buffer;
//now that i've read in 256 characters, i can search through them
buffer=strchr(buffer,']');
strcpy(buffer,parameters);
parameters=strtok(parameters,'[');//now that i have parameters from after the first ']', remove parameters from other categories by taking off everything after the next '[' (category)
strcpy(parameters,builderParameters);
delete [] parameters;
parameters=NULL;
char * parameters=new char[256];//clean variables to prevent memory leaks
buffer=strchr(buffer,'[');//go back to the buffer and fine the options section that was read from the ini file
//since i don't know if you have any more sections i'll just stop at that, otherwise repeat the strtok process of the parameters variable
strcpy(buffer,parameters);
strchr(parameters,']');//don't do parameters=... in this case because it would overwrite and spill memory, we just want to move through the variable to the ']' so that you just get your parameters, not "options]" included
strcpy(parameters,options);
//good luck
return 0;
}
kuphryn
September 19th, 2002, 03:23 PM
Approach the INI file as any other database text file. You read each line and find the value and update the variable.
Kuphryn
Sam Hobbs
September 21st, 2002, 02:25 PM
If it were me I would probably write some functions that used only the C++ Standard Classes (little or no C functions) and read the data into a map of maps. You could have a map of sections and for each section a map of names and values. The entire file could be read when the file is opened; then the function to get a value from a section would be very easy.
PaulWendt
September 22nd, 2002, 02:03 PM
Originally posted by Sam Hobbs
If it were me I would probably write some functions that used
only the C++ Standard Classes (little or no C functions) and read
the data into a map of maps. You could have a map of sections
and for each section a map of names and values. The entire file
could be read when the file is opened; then the function to get a
value from a section would be very easy.
This is an excellent suggestion since INI values typically have one
"key" to be associated with the "value". This method receives
my vote.
--Paul