zoukankan      html  css  js  c++  java
  • sscanf用法

    sscanf的作用:从一个字符串中读进于指定格式相符的数据。利用它可以从字符串中取出整数、浮点数和字符串。

    sscanf和scanf的区别:scanf是以键盘作为输入源,sscanf是以字符串作为输入源。

    sscanf:

    原型:

    int sscanf(const char *str, const char *format,......);

    说明:

    sscanf()会将参数str的字符串根据参数format字符串来转换格式并格式化数据。转换后的结果存于对应的参数内。

                                                  成功则返回参数数目,失败则返回0

    举例:

    1. 取指定长度的字符串

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("12345","%4s",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }
    10. 输出:1233

     

     

     

     2. 读入字符串

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("12345","%s",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }

     

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str1[100], str2[100], str3[100];
    6.        gets(str1);
    7.        sscanf(str1,"%s%s",str2,str3);
    8.        printf("%s %s ",str2,str3);
    9.        return 0;

    10.}

     

     

    3. %*d和%*s加了(*)表示跳过此数据不读入(也就是不把此数据读入参数中)

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("1234abcd","%*d%s",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }

     

     

    4.  取到指定字符为止的字符串。如例子所示,遇到‘+’为止的字符串。

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("1234+abc","%[^+]",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }

     

    遇到空格为止的字符串

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("1234+abc1234","%[^]",str);
    7.        printf("str=%s ",str);
    8.        return 0;
    9. }

     

     

     5. 取到指定字符集为止的字符串。如遇到小写字母为止的字符串。

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("1234+abc1234","%[^a-z]",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }

     

     6. 取仅包含指定字符集的字符串。(取仅包含数字和小写字母的字符串,是取得连续的字符串,貌似是到空格为止)。

    1. #include<stdio.h>
    2. #include<string.h>
    3. int main()
    4. {
    5.        char str[100];
    6.        sscanf("123456abcdefBFRGTY7890","%[1-9a-z]",str);
    7.        printf("%s ",str);
    8.        return 0;
    9. }

     

  • 相关阅读:
    搭建自己的博客(九):使用shell模式批量添加博客文章并增加分页功能
    搭建自己的博客(八):使用fontawesome框架来添加图标以及美化详情页
    linux系列(十):cat命令
    linux系列(九):touch命令
    搭建自己的博客(七):使用bootstrap框架美化导航栏
    linux系列(八):cp命令
    搭建自己的博客(六):添加首页,使用css对界面做美化
    linux系列(七):mv命令
    Re-enable extensions not coming from Chrome Web Store on Chrome v35+ (with enhanced security)
    liblensfun 在 mingw 上编译时遇到的奇怪问题
  • 原文地址:https://www.cnblogs.com/ziyuan122625/p/12000977.html
Copyright © 2011-2022 走看看