File stream seekg() does seem to be working.
Feb 12, 2012 at 12:33am UTC
Hello,
I use g++ on Ubuntu.
I open config file somewhere, then when I call GetInteger() function for the first time it works fine, when I call it second time it does not work. The problem is the get pointer is at the end of the file when I call GetInteger() for the second time (I know that from the len value).
Could someone help me with this? TIA!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
ReadConfigClass::ReadConfigClass(string str)
{
file_name = str;
file_stream.open(file_name.c_str(), ios_base::in | ios_base::out | ios_base::app);
if (!file_stream.is_open())
err("error opening config file." );
}
int ReadConfigClass::GetInteger(string key)
{
string line;
int value = -1;
string value_str;
file_stream.seekg(0, ios::beg);
file_stream.clear();
int len = file_stream.tellg();
while ( getline(file_stream, line) )
{
if ( line.substr(0,11) == key)
{
value_str = line.substr(12);
//value_str = ltrim(rtrim(value_str));
value = strtoi(value_str);
}
}
return value;
}
Feb 12, 2012 at 1:37am UTC
seekg will not work if any error flags are set so switch order on line 15 and 16.
Feb 12, 2012 at 1:40am UTC
ah, I have to clear() first. thank you!
Topic archived. No new replies allowed.