473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sscanf not working , why?

hi,
1) 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];
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);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?

while
2) second 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 å…°

how can i get the right result? thanks

baumann@pan

Nov 14 '05 #1
10 5469
also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )
{
char tokenstring[] = "15 12 14...";
char s[81];
char c;
int i;
float fp;

/* Input various data from tokenstring: */
sscanf( tokenstring, "%s", s );
sscanf( tokenstring, "%c", &c );
sscanf( tokenstring, "%d", &i );
sscanf( tokenstring, "%f", &fp );

/* Output the data read */
printf( "String = %s\n", s );
printf( "Character = %c\n", c );
printf( "Integer: = %d\n", i );
printf( "Real: = %f\n", fp );
}
Output

String = 15
Character = 1
Integer: = 15
Real: = 15.000000

i don't know what's the difference between the first example i give
above.

Nov 14 '05 #2
3) test program 3

#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 2nd example is the space between
the format string,

why ???

Nov 14 '05 #3
baumann@pan <ba*********@gm ail.com> wrote:
hi,
1) 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];
sscanf(file,"%s ",firststr) ;
printf("%s\n" ,firststr);
sscanf(file,"%i ",&i2);
here you give sscanf the same argument as before, it is the string
pointed to be file. Now sscanf will find 'a' to be the first character.
This is not a character that can be consumed by %i conversion. So sscanf
stops the conversion an returns.
If you check the return value of sscanf at this point you will see that
it is 0.

So the variables i2 an i3 remain uninitialized and may hold any value.

sscanf in contrast to fscanf and scanf does not consume the characters
of the string. The input string remains the same as befor it is passed
to sscanf.
printf("%i\n",i 2);

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

sscanf(file,"%c ", &thirdchar);
printf("%c\n",t hirdchar);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?

while
2) second 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 ?

how can i get the right result? thanks

baumann@pan


You really need to read the documentation of the sscanf function
carefully. This function is one of the hardest to understand and use in
the standard C library.

Note that %c only reads exactly one character not a string and also note
how a %s conversion stops at white space (which actually are characters
to). Depending on what you actually want to read from the input string
I could give you some more advice, but I don't recognize this from your
exaple programs for now.
--
Z (zo**********@w eb.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #4


Zoran Cutura wrote:
baumann@pan <ba*********@gm ail.com> wrote:
hi,
1) 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];
sscanf(file,"%s ",firststr) ;
printf("%s\n" ,firststr);
sscanf(file,"%i ",&i2);
here you give sscanf the same argument as before, it is the string

pointed to be file. Now sscanf will find 'a' to be the first character.
This is not a character that can be consumed by %i conversion. So sscanf
stops the conversion an returns.
If you check the return value of sscanf at this point you will see that
it is 0.

So the variables i2 an i3 remain uninitialized and may hold any value.

sscanf in contrast to fscanf and scanf does not consume the characters
of the string. The input string remains the same as befor it is passed
to sscanf.
thanks, i take it for grant, indeed it's not true,ie, characters are
not consumed.

printf("%i\n",i 2);

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

sscanf(file,"%c ", &thirdchar);
printf("%c\n",t hirdchar);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?

while
2) second 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 ?

how can i get the right result? thanks

baumann@pan

You really need to read the documentation of the sscanf function
carefully. This function is one of the hardest to understand and use in
the standard C library.

Note that %c only reads exactly one character not a string and also note
how a %s conversion stops at white space (which actually are characters
to).


indeed, when I delimit the each format string with space, the 2nd
program works well, why?
ie.
change
sscanf(file,"%s %i%i%c%f%s",fir ststr,&i2,&i3,& thirdchar,&f,la ststr);
to
sscanf(file,"%s %i %i %c %f
%s",firststr,&i 2,&i3,&thirdcha r,&f,laststr);

it prints what i want. thanks

Depending on what you actually want to read from the input string
I could give you some more advice, but I don't recognize this from your
exaple programs for now.
--
Z (zo**********@w eb.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond


Nov 14 '05 #5
baumann@pan wrote:
hi,
1) 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];
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);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?
Each sscanf() has as its first argument 'file' which has a constant address.

[...] how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after _ints;
sscanf(input_st ring, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%c", &character_afte r_ints);
printf("(space after second int) '%c'\n", character_after _ints);
sscanf(input_st ring + chars_seen + 1, "%c", &character_afte r_ints);
printf("(char after space) '%c'\n", character_after _ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'
Nov 14 '05 #6
baumann@pan wrote:
also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )

^^^^
Anything giving illegal definitions of main should not be used.
Nov 14 '05 #7


Martin Ambuhl wrote:
baumann@pan wrote:
hi,
1) 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];
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);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?
Each sscanf() has as its first argument 'file' which has a constant address.

[...]
how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after _ints;
sscanf(input_st ring, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%c", &character_afte r_ints);


why sscanf treat the delimit sign ' ' as an input when it encounter
format %c?
and read it and store it in the user provided variable?it's unusal.

do you read the 2nd post ??

in which i change the format string as

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);

it works well.

printf("(space after second int) '%c'\n", character_after _ints);
sscanf(input_st ring + chars_seen + 1, "%c", &character_afte r_ints);
printf("(char after space) '%c'\n", character_after _ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'


Nov 14 '05 #8


Martin Ambuhl wrote:
baumann@pan wrote:
hi,
1) 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];
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);
return 0;
}
the prog doesn't print 23 32 ,it emits the result

aaa
-1073748344
1073792608
a

why?
Each sscanf() has as its first argument 'file' which has a constant address.

[...]
how can i get the right result? thanks


#include <stdio.h>

int main(void)
{
char *input_string = "aaa 23 32 m 2.23 ammasd";
int first_int, second_int;
int chars_seen = 0, field;

char initial_string[23];
char character_after _ints;
sscanf(input_st ring, "%s%n", initial_string, &field);
chars_seen += field;
printf
("initial string: \"%s\", last field size: %d, chars consumed:
%d\n",
initial_string, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &first_int, &field);
chars_seen += field;
printf("first int: %d, last field size: %d, chars consumed: %d\n",
first_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%d%n", &second_int, &field);
chars_seen += field;
printf("second int: %d, last field size: %d, chars consumed: %d\n",
second_int, field, chars_seen);

sscanf(input_st ring + chars_seen, "%c", &character_afte r_ints);


why sscanf treat the delimit sign ' ' as an input when it encounter
format %c?
and read it and store it in the user provided variable?it's unusal.

do you read the 2nd post ??
in which i change the format string as

count =
sscanf(file,"%s %i %i %c %f
%s",fir*ststr,& i2,&i3,&thirdch ar,&f,la*ststr) ;
printf("count:% d, %s %i %i %c %f %s\n", count, firststr, i2,i3,
thirdchar,f,las tstr
);

it works well.
printf("(space after second int) '%c'\n", character_after _ints);
sscanf(input_st ring + chars_seen + 1, "%c", &character_afte r_ints);
printf("(char after space) '%c'\n", character_after _ints);
return 0;
}

Gives the following output. I'm sure you can figure it out.

initial string: "aaa", last field size: 3, chars consumed: 3
first int: 23, last field size: 3, chars consumed: 6
second int: 32, last field size: 3, chars consumed: 9
(space after second int) ' '
(char after space) 'm'


Nov 14 '05 #9
Martin Ambuhl wrote:

baumann@pan wrote:
also the msdn library vs.net 2003 has the example

#include <stdio.h>
void main( void )

^^^^
Anything giving illegal definitions of main should not be used.


I don't think that's illegal.
I think it falls under the clause

N869
5.1.2.2.1 Program startup
[#1]
"or in some other implementation-defined manner."

--
pete
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4784
by: whisper | last post by:
Hello: I am trying to write code to read in a bunch of lines from stdin, each containing a (variable) number of integers and writing each integer in a separate line to stdout. I came up the code below. I could not get sscanf to work because it does not increment its position
4
4258
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
82
12639
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so no Win32 specials allowed....
4
2029
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
6405
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less precise than I had expected:
22
2959
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
4
1811
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
21439
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
2313
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: <presence/absence of n space/tab on the first n lines> 12 <presence/absence of n space/tab here>0<presence/absence of n space/tab here>90 10 23 43 0 0 0 0 0 0 0 90 0 0 0 0 88 0 0 0 ...
0
9325
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9152
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
9930
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
9716
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
9571
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
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.