70,021
社区成员




sscanf(buf,"%d,%[^,],%d,%[^,],%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c);
#include <stdio.h>
int main(void)
{
char *buf="32275,sr,1,Qs,28135,56,94 ";
int pid,di,a,b,c;
char uid[10],ch[10];
sscanf(buf,"%d,%[^,],%d,%[^,],%d,%d,%d",&pid,uid,&di,ch,&a,&b,&c);
printf("%d,%s,%d,%s,%d,%d,%d",pid,uid,di,ch,a,b,c);
getchar();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAXWORD 1000
void teststring(char*str){
int a,b,c,d,e;
char buffer[MAXWORD];
char buffer2[MAXWORD];
char str1[MAXWORD];
char str2[MAXWORD];
int length = 0;
int i = 0;
int buflen = 0;
if(NULL == str)return;
length = strlen(str);
if(length >= MAXWORD)return;
/* get first int */
sscanf(str,"%d,%s", &a, buffer);
buflen = strlen(buffer);
/* get the ',' after the first string */
for(i = 0; i < buflen; i++){
if(buffer[i] == ','){
break;
}
}
if(i >= buflen)return;/* invalid input */
buffer[i] = 0;/* cut the ',' to string ending */
strcpy(str1, buffer);
/* get the second int */
sscanf(buffer+i+1,"%d,%s", &b, buffer2);
buflen = strlen(buffer2);
/* get the ',' after the second string */
for(i = 0; i < buflen; i++){
if(buffer2[i] == ','){
break;
}
}
if(i >= buflen)return;/* invalid input */
buffer2[i] = 0;/* cut the ',' to string ending */
strcpy(str2, buffer2);
/* get rest int */
sscanf(buffer2+i+1,"%d,%d,%d", &c, &d, &e);
printf("%d\n%s\n%d\n%s\n%d\n%d\n%d\n", a, str1, b, str2, c, d, e);
}
void main(){
teststring("32275,srasdasd,123131,Qsasdasda,28135,51236,94123");
return;
}
fscanf("%d,%s,%d,%s,%d,%d",&pid,&uid,&di,&ch,&a,&b,&c)
这样行不?