Advertisement Jump to content
Sign in to follow this  

the best way to parse a string of floats

This topic is 4934 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

Hi, i am reading in an array of floats that looks like this: point="-0.02125 -0.08879 -0.4612, 50.0 0.0 0.0, ....." and trying to put them in an array float data[50]; what is the best way of doing this? When i mean the easiest to read + most efficient, or the correct way it should be done. For example, one way would be to read strings between the commas one at a time, then use sscanf("%f",&element); or something, i'm sure theres a better way than that. Cheers Paul. using C++ btw

Share this post

Link to post
Share on other sites
a) Look carefully at the sscanf documentation: it has more features than people generally think.
b) What's the precise structure of your input string?
c) Look at std::stringstream.

Share this post

Link to post
Share on other sites
Original post by Muncher
i just love the "look in the documentation" answers....

What more did you expect at 3AM? I notice you haven't even bothered answering my question.

anyway, consider something similar to

sscanf("%f %f %f,", data+i, data+i+1, data+i+2);


std::istringstream iss;
iss >> data >> data[i+1] >> data[i+2];

But since you're not motivated in checking the docs, neither am I motivated to check for correctness.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!