Sign in to follow this  

the best way to parse a string of floats

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[i] >> 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this