学习博客:https://blog.csdn.net/yanyanwenmeng/article/details/82753014
具体看代码:
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int main() { char buf[512]; sscanf("123456","%s",buf); printf("%s ",buf); //输出123456 //取指定长度的zifuchuan 如下 去最大长度为4字节的字符串 sscanf("123456","%4s",buf); printf("%s ",buf); //输出1234 //遇到指定字符为止的字符串 如在下例取遇到空格为止字符串 sscanf("123456 abcde","%[^ ]",buf); printf("%s ",buf); //输出结果为 123456 遇到空格结束 sscanf("123456 abcd","%[^4]",buf); printf("%s ",buf); //输出结果为123 遇到4结束 //跳过此数据不读入 sscanf("123456abcd","%*d%s",buf); printf("%s ",buf); //输出结果为 abcd 跳过了整形 //取到指定字符集为止的字符串 sscanf("123456abcde","%[^a-z]",buf); printf("%s ",buf); //输出结果为123456 遇到a小写字母停止读入 sscanf("123456abcdDAD$^&&*","%[1-9a-z]",buf); printf("%s ",buf); //输出结果为123456abcdc 只读指定字符 return 0; }