Hello.
I'm trying to write simple configure file manager class.
Configure values are stored in a map<string, string> configDictionary
(first string is property, second value).
When some value in that map is changed, I want to write that new value in a configure file.
For that, I wrote a method, which is listed bellow:
Code:
typedef map<string, string>::iterator confIterator_d; // iterator
map<string, string> configDictionary;
string configFileName; //name of the config file
string lastError; //error msg
int ConfigManager::SaveConfigFile(const string &key)
{
fstream config;
config.open(configFileName.c_str(), ios::in | ios::out);
if (!config.is_open())
{
lastError = "Can't read file.";
return 1;
}
string tmp;
unsigned pos = 0;
while (getline(config, tmp))
{
size_t found = tmp.find(key);
if (found != string::npos)
{
confIterator_d it = configDictionary.find(key);
if (it != configDictionary.end())
{
tmp = it -> first + "=" + it -> second;
config.seekp(pos, ios::beg);
config.write(tmp.c_str(), tmp.length());
break;
}
}
pos += tmp.length();
}
config.close();
return 0;
}
key is a name of property which I want to save.
I'm reading file line by line. In each line
I'm looking for the key. If key is found, create new line (tmp)
and write it in the proper line of the file.
Question is, why it doesn't work? I just can't save the new value.