#### Archived

This topic is now archived and is closed to further replies.

# parsing info from a file into variables

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

## Recommended Posts

hi i have a text file that contains info about a simple tile map, and i''ve layed it out as such: -BEGIN- Level2Map 6 6 30.00 -TILES- 36 1 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 -TILETYPES- 2 GRASS FLOOR -TEXTURES- 2 media/grass.bmp media/floor.bmp -END- (im not sure if thats a bad way to lay it out for some reason, but hey its my first time any suggestions on improvment are welcome tho :D) anyways, im using C++ and i can write the file, and i can read each line into a char* i have some questions now though...once i have a line in a character array im having trouble spliting them up into seperate variables if that makes sense. For example, on the third line the string comes back as "6 6 30.0", but i want to break it up because each number represents a different value, so i want: width = 6 heigh = 6 size = 30.0 and the same for the grid of tiles, i want to put that into a 2d array could anyone help me with suggestions on how to do this please? or what functions i can use..i cant seem to find info on any type of tokenizer function like java, that would be good coz then i could seperate them by the white space. how can i split a string up by the whitespace? thanks *high five*

##### Share on other sites
Instead of reading each line in all at one time you can do something like this:

ifstream file( "blah" );

file >> val1;
file >> val2;
file >> val3;

val1 now contains the first number, val2 the second and so on.

-SirKnight

##### Share on other sites
Well, you could just write your own tokeniser. However, if you don''t want to, windows does come with one. I can''t remember the exact function names.

You have to remember that you''re unique, just like everybody else.

##### Share on other sites
std::string levelname_i_guess;int width, height;float size;std::ifstream fp(filename.c_str());	if(fp.fail())		return;	std::string linestring, key_str, meshname;	int cnt = 0;	while (!fp.eof())	{		fp >> key_str;		if (key_str == "-BEGIN-")		{                       fp >> levelname_i_guess;                       // and now to 6 6 30.00 line                       fp >> width;                       fp >> height;                       fp >> size;                }                else if (blah blah..)		{}         }

That's pretty much it I guess, mix of copy/paste and edit just to give the idea..

However I would suggest using XML file structure instead and use TinyXML for such things..

edit: damn code tag

[edited by - mentat on March 26, 2004 9:23:22 AM]

##### Share on other sites
I just use:

FILE *fp;

fscanf(fp."%d....", numberofetc...);

fclose (fp);

This works for me and i am using around 20 variables, the advantage of this method is that u can have any amount of white space and carriage returns betweent he individual data items and the scanning still works.

regards,

ace

##### Share on other sites
Instead of writing the data to a text file, write it to a binary file instead (ios::binary, in case you''re not aware of it). It''s so much easier to read information back in, once you''ve written the data, as you don''t really need to "parse" it as such, more validate that it''s correct.

-hellz

##### Share on other sites
thanks for the replies,
i am using character arrays for my text at the moment, what are the benifits of using std::string types? if any?

##### Share on other sites
you''ve given me a few different ways i am going to try tomorow, thanks everyone.

hellz: i am interested in how reading in a binary file is easier? i''ve been looking up some info on it, what does it mean by you read in each byte?
It seems to me (and im probly wrong!) that you can read in each byte, which i think is a character at a time isnt it? or u can read the whole file at once. if this is the case, wouldnt it be easier to use text format where i could just read upto the next whitespace, and then the next and the next...so on.

Sorry if these questions are a bit ''Duh'' but yeah, im still tryin to get my head around what can and cant be done when it comes to file i/o

##### Share on other sites
I would go for text file for some time, until I get it fully, then consider binary.. actually there is not much of a difference, since you load those stuff probably not every frame but in the level start.. so don''t worry about it, learn how to parse text files, everybody needs that.

and use std::string.. use STL whenever you can. at least for this set of problems. at least while learning. do not strive for optimization, yet.. less bugs, safe and fast coding.

good luck.

• ### Forum Statistics

• Total Topics
628728
• Total Posts
2984417

• 25
• 11
• 10
• 16
• 14