473,842 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
+ 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",i 2);
sscanf(file,"%i ",&i3);
printf("%i\n",i 3);
sscanf(file,"%c ", &thirdchar);
printf("%c\n",t hirdchar);

*/
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,las tstr);
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",i 2);
sscanf(file,"%i ",&i3);
printf("%i\n",i 3);
sscanf(file,"%c ", &thirdchar);
printf("%c\n",t hirdchar);*/
count = sscanf(file,"%s %i %i %c %f
%s",firststr,&i 2,&i3,&thirdcha *r,&f,laststr) ;
printf("count:% d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,las tstr);
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",fir ststr,&i2,&i3,& thirdcha*r,&f, laststr);

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

last, it works,
why?thanks

baumann@pan

Nov 14 '05 #1
4 2031
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
baumann@pan <ba*********@gm ail.com> wrote:
there has 2 program
1) the first test program code
sscanf(file,"% s%i%i%c%f%s",fi r=C2=ADststr,&i 2,&i3,&thirdcha r,&f,la=C2=ADst s=
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'.
--
"Mathematic s? I speak it like a native." -- Spike Milligan
Nov 14 '05 #2


Walter Roberson wrote:
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
baumann@pan <ba*********@gm ail.com> wrote:
there has 2 program
1) the first test program code
sscanf(file,"% s%i%i%c%f%s",fi r=C2=ADststr,&i 2,&i3,&thirdcha r,&f,la=C2=ADst s=
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'.
--
"Mathematic s? I speak it like a native." -- Spike Milligan


Nov 14 '05 #3
"baumann@pa n" <ba*********@gm ail.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_Keit h) 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@pa n" <ba*********@gm ail.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_Keit h) 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
5479
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
518
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 the value by
4
4265
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
5476
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
1819
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 while( fgets( command, MAXLLENGTH, stdin ) != NULL ) {
20
21454
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
3509
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%*s0%6s Heres the data: SS000000395000000000DC-07998291174503725 Actually, I would like to retrieve the "799829" from the data, but it always failed. I thought that the "%*s0" would work as if I was
9
2064
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 try this: $res = sscanf ($line, ' %s"%s"'); $wanted = $res;
4
1710
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 am trying to read a file with line line 8 characters wide, 88888888 it has unix line ending LF, but I am getting a segfault from the sscanf
0
9876
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9717
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10952
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10618
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10320
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5699
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4506
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 we have to send another system
2
4096
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.