Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

text file processing using string

Hello,

I am seeking a better solution to process a text file.
The task is, I want my C++ program to read various varibles given in a configuration text file that user can specify variuos varible parameters. These varibles have predetermined names that C can search in the text file, then following these variable names, C can identify what values should be given to them. For example, in the configuration text file called "cfg.txt", there are lines given as:
...
mode = 'automatic'
initial_value = [1.0 0.0 -2.5 0]
frequency = 250 Mhz
...

in this configuration text file, C function is expected to search the key names: mode, initial_value, and frequency, the running result should be given them values (char * or string) as 'automatice', vector as [1.0 0.0 -2.5 0], and double value as 250. Other lines can be anything just as comments.

Currently, I am using following:
ifstream fin("cfg.txt", ios::in);
if(!fin){
cerr<<"File could not be opened!\n";
exit(1);
}
struct input_para t;//input struct to store the read out values
char x[200];
fin.seekg(0,ios::beg);
while(!fin.eof()){
fin.getline(x,200);
if(x[0]=='\0') continue;
... // char by char to process/search line by line.
}
...

But I am not satisfied with this style, I want to use string and its library to give an efficient and more flexiable process.

Anybody has good suggestion to do that using STD string styles?
Thanks in advance.

Alvinw
[1643 byte] By [alvinw] at [2007-11-11 6:49:25]
# 1 Re: text file processing using string
the easiest way is to use the fin.read member function to read a fixed number of characters, or fin>>str to read a string from the file, delimited by a blank, newline etc.
Danny at 2007-11-11 21:02:54 >
# 2 Re: text file processing using string
Danny, Thank you.

Can you show me the way how to locate the variable name after the line has been read out as a text string and find out its corresponding value to assign to this name?

Regards,
Alvinw
alvinw at 2007-11-11 21:03:55 >