• Advertisement

Archived

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

Help with parsing a data file string.

This topic is 5863 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 need some help with parsing a data string. For my game, I''m have a bitmap file that contains all of the frames of the animation to display laid out sequentially. For each animation, there''s a line in a config-type file which contains various data about each animation to load. I have it in the following format: filename:frame width:frame height:frames eg., with animation file fire.bmp it might be: fire.bmp:48:48:8 How would I go about parsing that string into variables, using C? Would something like (assuming data_file and other variables have been set up properly): FILE *data_file; char image_file[256]; int width, height, frames; /* ... */ fscanf(data_file,"%s:%d:%d:%d",image_file,&width,&height,&frames); Is there a better, more efficient way of parsing data in a format similar to that which I''m using? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You could do something like this:

char separator = '':'';
char image_file[MAX_CHARS];
...
fgets(image_file, MAX_CHARS, data_file);
width = strtok(image_file, separator);
height = strtok(image_file, separator);
frames = strtok(image_file, separator);

Share this post


Link to post
Share on other sites
I think your original idea should work just fine.

"If patience is a virtue, and ignorance is bliss, you can have a pretty good life if you''re stupid and willing to wait"

Share this post


Link to post
Share on other sites
quote:
Original post by BitBlt
width = strtok(image_file, separator);
height = strtok(image_file, separator);
frames = strtok(image_file, separator);


Just a quick note on this: strtok requires you to use NULL as its first parameter on subsequent parses/extractions from the same string. Which would make your code the following:
width = strtok(image_file, separator);
height = strtok(NULL, separator);
frames = strtok(NULL, separator);

Just in case anybody needed that for future reference.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites

  • Advertisement