哎呀,这在c中是什么意思?
if ( sscanf( line, "%[^ ] %[^ ] %[^ ]", method, url, protocol ) != 3 )...
上面那个格式很奇怪,它是做什么用的?
if ( sscanf( line, "%[^ ] %[^ ] %[^ ]", method, url, protocol ) != 3 )...
That format above is very strange,what's it doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该行尝试将 3 个不包含空格分隔的字符串读取到 method、url、protocol 中,如果无法读取 3 个字符串,则会进入 if 块。
That line is attempting to read 3 strings that do not contain a space separated by spaces into method, url, protocol and if it fails to read 3 it will then enter the if block.
[]
是扫描集。如果您告诉%[abcd]
,则输入字符串仅包含 a 或 b 或 c 或 d 将被考虑。该字符串将在第一次出现某个其他字符时终止,该字符不是大括号中的任何字符。[]
内的^
用于表示大括号内集合的补集。与格式字符串%[^abcd]
一样,仅接受所有字符 除了 、 a 或 b 或 < em>c 或 d。因此,在
%[^ ]
中,^
后面的空格表明,格式字符串将接受字符串中的任何字符组合,其中 < em>没有有空格。格式字符串
"%[^ ] %[^ ] %[^ ]"
将匹配具有由空格分隔的三个组成部分的字符串。每个组件将包含一个字符序列,其中没有空格。该函数返回成功匹配和分配的输入项的数量,该数量可能少于提供的数量,如果早期匹配失败,甚至为零。
因此,仅当且仅当读取了所有三个组件时,上述函数才会返回
3
,即输入行具有三个分区,并且对于每个分区,三个数组方法
、url
和protocol
已填充。the
[]
is the scanset. If you tell%[abcd]
then an input string only with a or b or c or d will be considered. The string would terminate at the first occurance of some other character which is not any of the characters in the braces.The
^
inside the[]
is used to denote the compliment of the set inside the braces. Like with the format string%[^abcd]
will only accept all characters except , a or b or c or d.so in
%[^ ]
, the blankspace followed by the^
tells that, the format string will accept any character combination in the string which does not have a blankspace.The format string
"%[^ ] %[^ ] %[^ ]"
will match a string which has three components separated by blankspaces. Each of the component will contain a sequence of characters which have no space inside them.The function returns the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
So the above function will return
3
only if and only if all the three components are read, that is, the input line has three partitions and for each partition the three arraysmethod
,url
andprotocol
was populated.scanf 是一个从字符串中读取数据并返回项目数的函数成功读取。
因此,
scanf
将解析line
以找到3个包含空格并以空格分隔的字符串,并将这3个字符串放入后面的3个变量中(方法
、url
、协议
)。然后,如果它解析了 3 个参数,它将进入
if
块。scanf is a function that reads data from a string and returns the number of items successfully read.
So,
scanf
will parseline
to to find 3 string containing a space and separated by a space and will put these 3 strings in the 3 variables that follows (method
,url
,protocol
).Then if it has parsed 3 arguments, it'll enter the
if
block.