472,345 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

problem with sscanf.

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
4 1943
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


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
"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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d...
4
by: Ivan Lam | last post by:
Hi All, Thanks for reading my post! I have a problem that using the scanf function. I would like to scan a value from a line like: ...
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
by: lynx_ace | last post by:
Hi everyone. I need a little bit help here...I have an assignment and it is working fine until the last part which I can't solve. So here's the...
20
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*s■0■%6s Heres the data:...
9
by: Tim Streater | last post by:
I'm obviously missing something here. I have a line like this (in $line), and I want all the text within the double quotes:: somestr="string I...
4
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.