天天看點

分割字元串 sscanf的用法

當時博哥講的~~木有做好筆記~~導緻又重新翻了博哥的blog一遍才找到做法

#include<stdio.h>
char a[100]="17006 187 20 30 100 50 60";
int main()
{
    int num,add;
    char *p=a;
    while(sscanf(p,"%d%n",&num,&add)!=EOF){
        p=p+add;
        printf("%d %d\n",num,add);
    }    
}
           

能将裡面的整數取出來。

#include<stdio.h>
char a[100]="ppfasdf fasdf asdfa asdf afsd asdf asdf";
int main()
{
    int add;
    char *p=a;
    char ch[10];
    while(sscanf(p,"%s%n",ch,&add)!=EOF){
        p=p+add;
        printf("%s %d\n",ch,add);
    }    
    scanf("%d",&add);
}