读书人

关于sscanf()有关问题

发布时间: 2012-02-16 21:30:36 作者: rapoo

关于sscanf()问题
#include <stdio.h>
#include <string.h>

int main()
{
const char str[] = "hello,world ";
char Buf[10] = {0};
sscanf(str, "%*s%s ",Buf); //这段的%*s怎么把hello world全屏蔽了?
printf( "%s ",Buf);
getch();
return 0;
}

我想输出world,改怎么写?谢谢!:)

[解决办法]
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
const char str[] = "world ";
char Buf[10] = {0};
sscanf(str, "%s ",Buf);
printf( "%s ",Buf);
system( "pause ");
return 0;
}
[解决办法]
错了,上面的解释是正对printf的,关于sscanf的描述如下:
An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

这个地方错在对于字符串的分隔字符,字符串的分隔字符可以为:空格,TAB,回车。

改为下面就可以了:


#include <CONIO.H>
#include <stdio.h>
#include <string.h>

int main()
{
const char str[] = "hello, world ";
char Buf[10] = {0};
sscanf(str, "%*s%s ", Buf); //这段的%*s怎么把hello world全屏蔽了?
printf( "%s ",Buf);
getch();
return 0;
}


读书人网 >C语言

热点推荐