坚信科学,分享技术

sscanf函数的各种用法

sscanf是一个运行时函数,原形很简单: 
int sscanf( 
const char *buffer, 
const char *format [, 
argument ] … 
); 
它强大的功能体现在对format的支持上。 

sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。 

    char str[512] = ; 
  sscanf(“123456 ”, ”%s”, str); 
  printf(“str=%s”, str);

输出 str=123456

    sscanf(“123456 ”, ”%4s”, str); 
  printf(“str=%s”, str);

输出 str = 1234

  sscanf(“123456 abcdedf”, ”%[^ ]“, str); 
  printf(“str=%s”, str);

输出 str=123456 (遇到空格为止)

  sscanf(“123456abcdedfBCDEF”, ”%[^A-Z]“, str); 
  printf(“str=%s”, str);

输出 str=123456abcdef (遇到指定字符为止)

sscanf(mod, “%s”, mod);//去除空格

This entry was posted in linux, php and tagged , , , . Bookmark the permalink.

Leave a Reply

54chen on Twitter
Connect with 54chen on
Twitter QQ微博 sina微博 Profile
54chen(陈臻),人人网分布式研究人员,曾饭于金山、雅虎、淘宝,暂居北京,情绪稳定。曾用代号有:cc0cc、零蛋。

SEARCH

Archives