阅读使用sscanf的文本 [英] Read texts using sscanf
本文介绍了阅读使用sscanf的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题关于向sscanf函数的用法。
的#include<&iostream的GT;
#包括LT&;&stdio.h中GT;诠释的main()
{
烧焦一个[3];
炭B〔4〕;
为const char * p =aaaaaaaaaaaaaaaaaa BBBBBB
sscanf的(P,%2S%3S,A,B);
性病::法院LT&;< A<<的std :: ENDL;
性病::法院LT&;< B<<的std :: ENDL;
返回0;
}
所需的输出将是:
AA
BBB
实际输出为:
AA
AAA
解决方案
如果你想跳过所有的字符,直到达到一个空间,使用%* [^]
:
的sscanf(P,%2S%* [^]%3S,A,B);
之后的星号%
告诉的sscanf
忽略输入的相应部分。
这将产生所需的输出(演示1 )。
为什么这种情况下不工作?
的char a [5];
炭B〔6〕;
为const char * p =字字词2,单词;
sscanf的(P,%4S%* [^]%5S,A,B);
块引用>这不起作用,因为
%S
和%[...]
不要混用很好。替换%氯化钠
与%N [^]
:的sscanf(P,4%[^]%* []%5 [^],A,B);
演示2 。
I have a question regarding to usage of sscanf function.
#include <iostream> #include <stdio.h> int main() { char a[3]; char b[4]; const char* p = "aaaaaaaaaaaaaaaaaa bbbbbb"; sscanf(p, "%2s %3s", a, b); std::cout << a << std::endl; std::cout << b << std::endl; return 0; }
The desired output would be: aa bbb
Actual output is: aa aaa
解决方案If you would like to skip all characters up to a space, use
%*[^ ]
:sscanf(p, "%2s%*[^ ] %3s", a, b);
The asterisk after
%
tellssscanf
to ignore the corresponding portion of the input.This produces the desired output (demo 1).
why this case is not working?
char a[5]; char b[6]; const char* p = "word word2 , word"; sscanf(p, "%4s%*[^ ] %5s", a, b);
This does not work because
%s
and%[...]
do not mix very well. Replace%Ns
with%N[^ ]
:sscanf(p, "%4[^ ]%*[ ]%5[^ ]", a, b);
这篇关于阅读使用sscanf的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文