problem with sscanf.

B

baumann@pan

hi all
there has 2 program
1) the first test program code


#include <stdio.h>


int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;


float f;
char firststr[23];
char thirdchar;
char laststr[23];
int count;
count;
/*

sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);


sscanf(file,"%i",&i3);
printf("%i\n",i3);
sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);

*/


count =
sscanf(file,"%s%i%i%c%f%s",fir­ststr,&i2,&i3,&thirdchar,&f,la­ststr);

printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);
return 0;

}


gives me the result

count:4, aaa 23 32 36.759842 å…°







2) test program 2


#




- Hide quoted text -
- Show quoted text -

include <stdio.h>
int main(void)
{
char * file = "aaa 23 32 m 2.23 ammasd";
int i2,i3;
float f;
char firststr[23];
char thirdchar;
char laststr[23];
int count;
count;
/*sscanf(file,"%s",firststr);
printf("%s\n" ,firststr);
sscanf(file,"%i",&i2);
printf("%i\n",i2);
sscanf(file,"%i",&i3);
printf("%i\n",i3);
sscanf(file,"%c", &thirdchar);
printf("%c\n",thirdchar);*/
count = sscanf(file,"%s %i %i %c %f
%s",firststr,&i2,&i3,&thirdcha­r,&f,laststr);
printf("count:%d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,laststr);
return 0


;


}


this give me the right result,

count:6, aaa 23 32 m 2.230000 ammasd


but the only difference between the first example is the space between
the format string,

i.e.
i changed

count =
sscanf(file,"%s%i%i%c%f%s",firststr,&i2,&i3,&thirdcha­r,&f,laststr);

to


count = sscanf(file,"%s %i %i %c %f
%s",firststr,&i2,&i3,&thirdcha­r,&f,laststr);

last, it works,
why?thanks

baumann@pan
 
W

Walter Roberson

there has 2 program
1) the first test program code
sscanf(file,"%s%i%i%c%f%s",fir=C2=ADststr,&i2,&i3,&thirdchar,&f,la=C2=ADsts=
tr);

The %c format does NOT skip leading spaces, and the %i before it
leaves you positioned at the space immediately after the number. Thus
the %c is reading the space and everything after that is off by one
field.

In your second program where you had a space before the %c then that
matched the space after the number and everything was then properly
aligned for the %c to read the 'm'.
 
K

Keith Thompson

baumann@pan said:
Walter Roberson wrote: [...]
The %c format does NOT skip leading spaces [...]

thanks much, C99 stander has mentioned it?

Of course. Both the C90 and C99 standards clearly state this, as do
K&R and H&S. The documentation that came with your implementation
should explain this as well. If it doesn't, you should complain to
the vendor.
 
B

baumann@pan

Keith said:
baumann@pan said:
Walter Roberson wrote: [...]
The %c format does NOT skip leading spaces [...]

thanks much, C99 stander has mentioned it?

Of course. Both the C90 and C99 standards clearly state this, as do
K&R and H&S. The documentation that came with your implementation
should explain this as well. If it doesn't, you should complain to
the vendor.
yes i find it in c99 standard.

but in college, the textbook doest mention it. i think in china few
people

knows the ISO c/c++ standards. mostly we buy a textbook find what need
to know.


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top