我正在努力学习一些CI认为它是什么?我正在查看sscanf的以下代码
sscanf(val1,":%[^ ] %s %s %[^\n]%*[\n\r]%n", val2 , val3 , val4 , val5 ,&n);
Run Code Online (Sandbox Code Playgroud)
但是我在理解它时遇到了很多麻烦,我在网上看了sscanf,据我所知,val1中的char数组被切割成val2,val3,val4,val5和&n.但我不太明白这一点:
:%[^ ] %s %s %[^\n]%*[\n\r]%n
Run Code Online (Sandbox Code Playgroud)
我假设字符串根据%s,部分被切断,但其余的我不太确定,我真的希望有人可以给我说一个示例字符串,以及如何变成不同的值?谢谢!!
格式字符串解析如下:
:%[^ ] %s %s %[^\n]%*[\n\r]%n
^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | |
| | | | | | | +-- the number of characters read so far
| | | | | | +------ \n or \r
| | | | | +---------- read and ignore this portion of the input
| | | | +-------------- Read string up to \n; should be [^\n\r]
| | | +-------------------- Read a string
| | +---------------------- Read a string
| +--------------------------- Read a string up to a space character
+------------------------------ Read a ':' character
Run Code Online (Sandbox Code Playgroud)
单个空格(如果存在)表示需要消耗零个或多个空格字符的序列,而不是放入任何输出变量.例如,"%s %s"应用于"hello world"字符串将"hello"放入第一个字符串,并"world"放入第二个字符串,并忽略分隔这两个字符串的空格.
该%n格式是存在的,这样你能告诉多少个字符已经从该操作的输入消耗.这通常用于调整循环中下一次读取的位置,并确定循环是应该继续还是终止.
注意:使用%s是不安全的,因为输入足够长的字符序列会导致缓冲区溢出.当你读一个字符串转换为长的缓冲区N,你应该把大小N-1之间%和s.例如,如果将字符串读入大小为32的缓冲区,请更改格式字符串,如下所示:
:%31[^ ] %31s %31s %31[^\n]%*[\n\r]%n
Run Code Online (Sandbox Code Playgroud)
这可确保在用户输入超过31个字符时没有缓冲区溢出.需要为空终止符保留字符32.
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |