Jump to content
  • Advertisement
Sign in to follow this  
_Madman_

String reading from fstream

This topic is 4875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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???

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Madman_
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?


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].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!