70,022
社区成员




char sex[3]
while(scanf("%s",sex)!=1) fflush(stdin);
$ cat test.c && gcc -o demo.exe test.c && ./demo.exe 5
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char** args)
{
unsigned int n;
char* buf,ws, format[32];
if(argc == 2)
{
if(sscanf(args[1], "%i", &n) != 1 || 0 == n)
return -1;
buf = (char*) malloc(n+1);
sprintf(format, "%%%is%%c", n);
if(buf)
{
do
{
if(scanf(format, buf, &ws) == 2)
{
if(strlen(buf) == n && ws == '\n')
{
break;
}
}
fflush(stdin);
}while(1);
printf("you finally get: %s", buf);
free(buf);
}
}
return 0;
}
//123
//1234
//123456
//12345
//you finally get: 12345
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
p=s;
while (1) {
k=sscanf(p,"%d%n",&v,&n);
printf("k,v,n=%d,%d,%d\n",k,v,n);
if (1==k) {
p+=n;
} else if (0==k) {
printf("skip char[%c]\n",p[0]);
p++;
} else {//EOF==k
break;
}
}
printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
char sex[3]
printf("性别:");
while(scanf("%2s",sex)!=1) fflush(stdin);