Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4242011
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-03-25 09:54:42

1.相关头文件:
  1. #include<stdio.h>

2.函数表达式
  1. int sscanf(const char *str,const *format[...])

3.参数详解
  1. 参数str为目标字符数组
  2. 参数 format为指定的格式输入字符串,该字符中,指定输入数据的格式,每一个数据格式以字符%开始之控制字符结束
  3. 参数argument为输入数据的接受地址

4.返回值详解
  1. 函数执行成功返回正确取值的参数个数,失败返回-1,并设置全局错误变量errno

5.函数详解
  1. 函数根据参数format中指定的格式在参数str字符串中读取数据,并转换成相应的数据类型存到参数argument中

6.操作实例
  1. 使用函数 sscanf将字符串中的数据解析出程序需要的数据,并打印在标准输出中

  1. #include <stdio.h>

  2. #define GNU ""
  3. #define GOOGLE ""

  4. #define FORMAT ".%[a-z]."
  5. #define BUF_SIZE 128

  6. int main(void)
  7. {
  8.     char host[BUF_SIZE];
  9.     int i;

  10.     for(i=0;i<BUF_SIZE;i++)
  11.     {
  12.         host[i]='\0';
  13.     }
  14.     
  15.     if(sscanf(GNU,FORMAT,host)==-1)
  16.     {
  17.         perror("failed sccanf");
  18.         exit(1);
  19.     }

  20.     printf("first str host name is:%s \n",host);

  21.     for(i=0;i<BUF_SIZE;i++)
  22.     {
  23.         host[i]='\0';
  24.     }

  25.     if(sscanf(GOOGLE,FORMAT,host)==-1)
  26.     {
  27.         perror("failed sscanf");
  28.         exit(1);
  29.     }

  30.     printf("second str name is :%s \n",host);
  31.     
  32.     exit(0);
  33. }








阅读(1733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~