开发者

sscanf reads string in ( )

this question bothers me this few days. I want to read a string for example, input = (-0.001999,-0.919191,-0.777777,999999.999999) using sscanf. And my code is

char x[10], y[10], z[10], angle[10];
sscanf( input+1, "%[^,],%[^,],%[^,],%[^)]", &x, %y, &z, &angle);
printf("%s %s %s %s\n", x, y, z, angle);

the expected result is x=-0.001999 y=-0.919191 z=-0.777777 a=999999.999999

but my printf shows : x=-0.001999 y=-0.919191 z=999 a=999999.99999开发者_Python百科9

can somebody help me figure out where the problem is ??

thx!


angle is not big enough to hold the input data and thus z is getting overwritten. Try this:

char x[80], y[80], z[80], angle[80];


After fixing the odd buffer overflow and such, I get:

char input[] = "(-0.001999,-0.919191,-0.777777,999999.999999)";
char x[10], y[10], z[10], angle[14];
sscanf( input+1, "%9[^,],%9[^,],%9[^,],%13[^)]", x, y, z, angle);
printf("%s %s %s %s\n", x, y, z, angle);

and the output I get from this is:

-0.001999 -0.919191 -0.777777 999999.999999


Your sscanf like should be sscanf( input+1, "%[^,],%[^,],%[^,],%[^)]", x, y, z, angle); since your arrays are already pointers (and the y has a % in your example). The other responder has solved the root problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜