By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

problem with sscanf.

P: n/a
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,&thi rdchar,&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,&third cha¬*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

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <11**********************@g49g2000cwa.googlegroups .com>,
baumann@pan <ba*********@gmail.com> wrote:
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'.
--
"Mathematics? I speak it like a native." -- Spike Milligan
Nov 14 '05 #2

P: n/a


Walter Roberson wrote:
In article <11**********************@g49g2000cwa.googlegroups .com>,
baumann@pan <ba*********@gmail.com> wrote:
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


thanks much, C99 stander has mentioned 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'.
--
"Mathematics? I speak it like a native." -- Spike Milligan


Nov 14 '05 #3

P: n/a
"baumann@pan" <ba*********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

P: n/a


Keith Thompson wrote:
"baumann@pan" <ba*********@gmail.com> writes:
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.