• Advertisement

Archived

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

Parsing a data string into variables, with C.

This topic is 5923 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
Well, that''s a little off. You need to convert your ascii text into integers with atoi(). You would also probably want a way to know which file you are loading, or to search for one. Assuming one file per line, this could be easy to traverse the file and look for the correct file and load the values in.

(http://www.ironfroggy.com/)
(http://www.ironfroggy.com/pinch)

Share this post


Link to post
Share on other sites
First, let's get the language straight.
You don't need atoi() if you're using fscanf();, if you were
using fread(); then you would need atoi();. fscanf(); works
just like scanf but reads in a file.

Second, if you're gonna use fscanf();, you might be able to
parse them using colons but I'm pretty sure they need to
be delimited by whitespace(spaces, tabs, linefeeds, newlines,
carriage returns),

"fire.bmp 48 48 8"

or such and doing one file per line would be entirely up to
you, fscanf(); only needs the whitespace. I've done something
similar to this before so be sure to heed this advice: try it
on some test data; fscanf(); can be tricky and I got some
really odd run-time errors. If worse comes to worse, just
add another space in between every element you want to read in
if you use whitespace to separate them that is.

Edited by - RolandofGilead on February 3, 2002 5:03:39 PM

Share this post


Link to post
Share on other sites

  • Advertisement