独立博客: 哲学与程序

哲学与程序

sscanf and scanf函数%[]用法

scanf函数中的%[]用法:
    %[]表示要读入一个字符集合, 如果[后面第一个字符是”^”,则表示反意思。
    []内的字符串可以是1或更多字符组成。空字符集(%[])是违反规定的,可导致不可预知的结果,同样%[^]也是违反规定的。
例如: %[a-z]读取在a-z之间的字符串,如果不在此之前则停止,如
          char s[]="hello, my friend” ; // 注意: ,逗号在不 a-z之间
          sscanf( s, “%[a-z]”, string ) ; // string=hello
      %[^a-z] 读取不在a-z之间的字符串,如果碰到a-z之间的字符则停止,如
          char s[]="HELLOkitty” ; // 注意: ,逗号在不 a-z之间
          sscanf( s, “%[^a-z]”, string ) ; // string=HELLO
      %*[^=] 前面带 * 号表示不保存变量,跳过符合条件的字符串。
          char s[]="notepad=1.0.0.1001" ;
          char szfilename [32] = "" ;
          int i = sscanf( s, "%*[^=]", szfilename ) ; // szfilename=NULL,因为没保存
          int i = sscanf( s, "%*[^=]=%s", szfilename ) ; // szfilename=1.0.0.1001

posted on 2011-01-15 19:54 哲学与程序 阅读(562) 评论(1)  编辑 收藏 引用 所属分类: C & C++

评论

# re: sscanf and scanf函数%[]用法 2011-01-16 00:27 jhiojio

454454454545
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

公告

欢迎访问 http://zhexue.sinaapp.com

常用链接

随笔分类(37)

随笔档案(41)

Algorithm

最新随笔

搜索

最新评论

独立博客: 哲学与程序