sscanf输入总结

2017-08-21 15:09:47

writer:pprp

sscanf很好用的,一般配合gets()来使用

/*
theme: usage of sscanf
writer:pprp
date:2017/8/21
declare:sscanf的基本用法总结
*/

#include <bits/stdc++.h>

using namespace std;

char s[] = "abcdefghijklmn";

int main()
{
      char str[10010];
      
      //s如果是string类型的就不行
      //用来取指定长度的字符串
      sscanf(s,"%5s", str);
      printf("%s
",str);
      
      //从a开始的之后的字符
      sscanf(s,"a%s", str);
      printf("%s
", str);
      
      // 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
      sscanf("123456 78910","%[^ ]",str);
      printf("%s
",str);
      
      //取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
      sscanf("123445KUTYFHGasd788765asdffdsafdsIUYFF1234SRF","%[1-9a-z]",str);
      printf("%s
",str);
      
      //取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
      sscanf("12344321asdffdaASDFFDSA","%[^A-Z]",str);
      printf("%s
",str);
      
      //给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
      //*代表跳过此数据不读入. (也就是不把此数据读入参数中)
      sscanf("iios/12DDWDFF@122","%*[^/]/%[^@]",str);
      printf("%s
", str);
      
      //给定一个字符串““hello, world”,仅保留world。(注意:“,”之后有一空格)
      //第一个被读入的串被过滤掉了
      sscanf("hello world","%*s%s",str);
      printf("%s
",str);
      
      
      return 0;
}

 

原文地址:https://www.cnblogs.com/pprp/p/7404542.html