Search

4/03/2006

sscanf

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);
}
// output: (1 1 1)


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.

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

strcpy(arr, "1 2 3");
sscanf(arr, "%d %[^n]", &i, arr);
//每次都把%[^n]的結果寫回arr
sscanf(arr, "%d %[^n]", &j, arr);
sscanf(arr, "%d %[^n]", &k, arr);
printf("(%d %d %d)n", i, j, k);
}
// output: (1 2 3)


[about using "sscanf" :=)]

[problem with sscanf()]

沒有留言: