首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于sscanf()有关问题

2012-02-16 
关于sscanf()问题#includestdio.h#includestring.hintmain(){constcharstr[]hello,world charBuf[

关于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;
}


热点排行