String reading from fstream

Started by
3 comments, last by Zahlman 18 years, 11 months ago
I'm currently working on fstreams, strings to read config file for my engine, what bugz me is - do I code right. Example:

fstream f;
string buf;
f.open("config.ini", ios::in);
while(!f.eof())
{
f.getline(&buf, INT_MAX, '\n');
//process
}


As far as I know string class is simple char*, but masked under some sort of interface. If I need to read INT_MAX chars to string there might be buffer overrun... As far as I've seen there are some methods and variables connected with string active buffer lenght/override/alloc, but maybe fstream take care of them... BTW, I am also wondering why most of game engines stick with config file structure as follows:

%comment
parameter0=value;
paramerer1=value, value;
They don't seem to be easiest to read, parse... Am I wrong? So far I can imagine:

f.getline(&str, INT_MAX, '=');
if(str==parameter0)
{
f.getline(&str, INT_MAX, '\n');
//process
}
else if(str==parameter1)
{
f.getline(&str, INT_MAX, '\n');
//process
}


as the best approach, is there a better one? What about comments, do I read first char then rewind???
______________________________Madman
Advertisement
Noone uses strings???
______________________________Madman
I'm a naughty monkey, since I don't actually use strings for that sort of thing.

Though I'd recommend to use standalone getline
getline([istream],[string],[delimiter='\n']);

instead.

And it's generally easier to do your parsing after reading in the entirety [or a sizeable chunk] of file.
Quote:Original post by _Madman_
BTW, I am also wondering why most of game engines stick with config file structure as follows:
%commentparameter0=value;paramerer1=value, value;

They don't seem to be easiest to read, parse... Am I wrong?


Well if you have a file format such as that, and the values are delimited by the semicolon and equals sign, you can use a function such as this one to easily parse a file into tokens, then run though the vector and set your settings.

So for example if you have some file that looks like this:
Width = 100;
Height = 50;

And you set the delim string to " ;=", you will end up with a vector that looks like this:
Width
100
Height
50

To which you can just do something like:

if( itr = vector.find("Width") != vector.end() )
Width = atoi( (*++itr) );

But there are many other ways of course to do file parsing [smile] Oh and I agree with Telastyn on using that standalone getline function instead [wink].
Yes. The free-standing getline is required to read in to std::string objects. These are actually fairly elaborate wrappers for a char * (they are not themselves pointers after all), and so there is a definite type mismatch there.

The stream member function getline can still be used to read into a character buffer (e.g. a local char[], or allocated space pointed at via a char*). The usual caveats about "C-style string manipulation" apply, of course. It's generally far more effort than it's worth.

This topic is closed to new replies.

Advertisement