Jump to content
  • Advertisement
Sign in to follow this  
papa

sscanf - pattern matching

This topic is 5136 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 trying to extract from a line of text eg: "a( -0.500001, 2.23233 ) b( 2.32222, -3.44444 )" all these 4 numbers and store them into float variables. see my code below.

void main()
{

struct Vector2D{ float x; float y;};

Vector2D a, b;

// I am reading only the first part "a( %f"  of the long text line and it does read it correctly. 
if (sscanf("a( -0.500001, 2.23233 )	b( 2.32222, -3.44444 )", "a( %f", &a.x) == 1)
{
	cout << a.x << endl; // its ok :)
}

// however when I read more stuff (after comma another number) then it fails reading. 
//It wont read data correctly and enter the if statement.. why????
if (sscanf("a( -0.500001, 2.23233 )	b( 2.32222, -3.44444 )", "a( %f, %f ) b( %f, %f )", &a.x, &a.y, &b.x, &b.y) == 1)
{
	cout << a.x << " " << a.y << "\t\t" << b.x << " " << b.y << endl;   // it never enters this if statement.
	}
}




I have to read somehow all four digits into float variables. What is wrong with the second statement? Another Question: Are there any other alterantives equally clever and short as sscanf in C++ so I could easily extract those digits from a line of text? Please let me know! thanks!!

Share this post


Link to post
Share on other sites
Advertisement
scanf style methods return the number of matches found. So your second statement will return 4 on success, not 1, since you're getting 4 values.

Share this post


Link to post
Share on other sites
oouuppss. I didnt notice this. thank you very much! it works now.

I would like to ask one more question if I could. Why this wouldnt work?


// this fails as I am trying to match the second part "B( %f, %f )"
if (sscanf("a( -0.500001, 2.23233 ) b( 2.32222, -3.44444 )", "b( %f, %f )", &b.x, &b.y) == 2)
{
cout << b.x << " " << b.y << endl;
}

I understand the string doesnt start with "b(" but its starts with "a(" but I thought it would search the entire string to find "b(" then read in floats. Its really hard to find some documentation on sscanf showing how it works. All I found is that "%*s" could be used to ignore parts of strings but I am not sure how. Well I am not sure whether this would do solve my problem but basically I am trying to read only second part of the string and ignore the first part.

[Edited by - papa on September 4, 2004 11:40:47 AM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!