zoukankan      html  css  js  c++  java
  • sscanf

    int sscanf(const char* str, const char* format, ...)
    functions: Reads data from str and stores them according to the parameter format into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in the format string.

    In the format:
    Whitespace character: the function will read and ignore any whitespace characters which are encounterd before the next non-whitespace character.
    Non-whitespace character: except percentage signs(%): any character that is not either a whitespace character or part of a format specifier causes the function to read the next character from str, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format and str. If the character does not match, the function fails and returns.
    Return Value:
    On success. the function returns the number of items successfully read.
    On failure: In the case of an input failure before any data could be successfully read, EOF is returnded.

       //默认以空格分隔str为不同字符串,返回正确输入的变量个数 
       char buf1[512= {0};
       cout  
    << sscanf("abc def gh","%s",buf1) <<endl;   //1
       cout << buf1 << endl; //abc 
       
       
    //可以使多个一起存入 
       char buf2[512= {0};
       
    char buf3[512= {0};
       cout 
    << sscanf("abc 2def gh","%s%s",buf2,buf3) <<endl;  //2
       cout << buf2 << endl; //abc 
       cout << buf3 << endl;//2def
       
       
    //空格或format格式以外的字符如果匹配则与前一个字符串一起输入,否则停止,退出 
       char buf4[512= {0};
       
    char buf5[512= {0};
       cout 
    << sscanf("abc 2def gh","%s,%s",buf4,buf5) <<endl; //1   
       cout << buf4 << endl; //abc
       cout << buf5 << endl; //nothing

       
    char buf6[512= {0};
       
    char buf7[512= {0};
       
    //注意两个%s之间的空格不可少  
       sscanf("abc 2def gh","%s 2%s",buf6,buf7);   
       cout 
    << buf6 << endl; //abc
       cout << buf7 << endl; //def   
       
       
    //当然不只是字符串形式的,其他也都可以 
       char buf8[512= {0};
       
    int a;
       cout 
    << sscanf("abc 2def gh","%*s %d",&a) <<endl;//1
       cout << a << endl;//2
       
       
    //Failure 
       int b;
       cout 
    << sscanf("abc","%d",&b) <<endl; //0



    幸运草 2010-04-25 20:13 发表评论
  • 相关阅读:
    Android 设计一个可以移动的小球,当小球被拖到一个小矩形块中时退出程序
    Android canvas+paint绘制一个可以指定位置移动的小球(含触屏响应)
    Android ImageView 实现图片触屏左右、上下以及按钮切换图片
    Android Mediaplay 音乐播放器(项目中的音乐)
    第二章课后习题 Q3
    第二章课后练习 Q2、4
    第二章课后练习 Q1
    算法学习-----01背包问题
    C#设计模式--迭代器模式(学习Learning hard设计模式笔记)
    C#设计模式--命令模式(学习Learning hard C#设计模式笔记)
  • 原文地址:https://www.cnblogs.com/liyuxia713/p/2540788.html
Copyright © 2011-2022 走看看