輸入的字串可是是"
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv){
int b[5] = {0, 0, 0, 0, 0};
int ret;
if(argc == 2){
ret = sscanf( argv[1], "%d,%d,%d,%d,%d\n", &b[0], &b[1], &b[2], &b[3], &b[4]);
printf("ret = %d, b0=%d b1=%d b2=%d b3=%d b4=%d\n", ret, b[0], b[1], b[2], b[3], b[4] );
}
}
執行狀況如下:
./test
./test 1
ret = 1, b0=1 b1=0 b2=0 b3=0 b4=0
./test 1,2
ret = 2, b0=1 b1=2 b2=0 b3=0 b4=0
./test 1,2,3
ret = 3, b0=1 b1=2 b2=3 b3=0 b4=0
./test 1,2,3,4
ret = 4, b0=1 b1=2 b2=3 b3=4 b4=0
./test 1,2,3,4,5
ret = 5, b0=1 b1=2 b2=3 b3=4 b4=5
./test 1,2,3,4,5,6
ret = 5, b0=1 b1=2 b2=3 b3=4 b4=5
sscanf的函式宣告型態如下
int sscanf(const char *str, const char *format, ...);
使用它時需要
#include <stdio.h>
沒有留言:
張貼留言