473,465 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2011
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 target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
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: file:c:\program files\mpd\mpd.exe however, when I read...
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 code in simple form #define maxlength 200 ...
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 2004? Thanks Mike
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: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
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 want" with some whitespace at the beginning. So 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 not figure out my problem. Simple code is below, I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.