about using "sscanf" :=)

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

about using "sscanf" :=)

Post by hank »

[c]#include <stdio.h>
#include <string.h>
void main()
{
char arr[80];
int i,j,k;
strcpy(arr,"1 2 3");
sscanf(arr,"%d",&i);
sscanf(arr,"%d",&j);
sscanf(arr,"%d",&k);
printf("( %d %d %d )",i,j,k);
}[/c]
Can you tell me why "j != 2" and "k != 3" ?
I want to convert the string "arr" to int ?
How to modify my code?

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil »

hi. i would use the following code:
[c]#include <stdio.h>
#include <string.h>

void main()
{
char arr[80];
int i,j,k;

strcpy(arr, "1 2 3");
sscanf(arr, "%d %[^\0]", &i, arr);
sscanf(arr, "%d %[^\0]", &j, arr);
sscanf(arr, "%d %[^\0]", &k, arr);

printf("(%d %d %d)\n", i, j, k);
}
[/c]
in your example, when you take the first input (i) from the string, the string itself (arr) is never changing. so whenever you take another input (j) from the same string (arr) , they (i and j) will be the same.

i think i saw in some post someone mentioned that this can be done in an easier way by using something like strstream in C++.

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

Post by hank »

Thanks a lot!!but what's the meaning of "%[^\0]"?
:P

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

or when you use strtok() in C ;-)

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise »

hank wrote:Thanks a lot!!but what's the meaning of "%[^\0]"?
:P
%[^\0] means "take all character until you find \0". It's a bit like %s, but %s stops when it finds a space or a new line. %s is equivalent to %[^ \n\r].
With that trick, you read a %d from attr, then you read the end of attr and store it IN ATTR, and as a consequence when you'll next read "%d" from attr, you'll get "2", not "1" again.

PS to Adil : are you sure one could never have a trouble, reading from attr and writing to it anew, on certain operating systems ? Wouldn't it be safer (though slower) to read to a temporary buffer, then to strcpy ?

jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post by jakabjr »

actually, "%s" skips all preceeding blank spaces (specified in isspace() for instance), which are more then " ", "\n" and "\r". (the tab is a blank surely "\t").
as for reding from a string, here's a good topic, which gives u all the solutions u need:
http://acm.uva.es/board/viewtopic.php?t ... 431b7e32aa
Understanding a problem in a natural way will lead to a natural solution

Post Reply

Return to “C”