简体   繁体   English

C 语言:scanf 和 sscanf 表达式

[英]C language: scanf and sscanf expressions

I've encountered a expressions that go something like this inside scanf and sscanf arguments:我在 scanf 和 sscanf 参数中遇到过类似这样的表达式:

sscanf(buffer, "%d,%100[^,]%*c%f", destination_pointer)

or或者

scanf("\n%99s", destination);

What is the correct way of interpreting these?解释这些的正确方法是什么? I know what things like "%s %c %d" are, and also that the %100 or generally "%number" is the size of the input to be read.我知道“%s %c %d”之类的东西是什么,而且 %100 或通常的“%number”是要读取的输入的大小。 But what about the rest?但是剩下的呢? All I can find are basic examples, nothing near this complex.我能找到的只是基本的例子,没有接近这个复杂的东西。 Is there any reference guide?有参考指南吗?

What is the correct way to interpreted these?解释这些的正确方法是什么?

sscanf(buffer, "%d,%100[^,]%*c%f", destinantion_pointer) sscanf(buffer, "%d,%100[^,]%*c%f", destination_pointer)

Is an invalid call.是无效调用。 There are 3 conversion specifiers that need an argument - %d , %[] , %f .有 3 个需要参数的转换说明符 - %d%[]%f That means exactly 3 arguments after formatting string are needed, but only one destinantion_pointer is provided.这意味着在格式化字符串后正好需要 3 个参数,但只提供了一个destinantion_pointer

  • %d - ignore any whitespace characters, read an int in base 10 %d - 忽略任何空白字符,读取以 10 为基数的int
  • , - read a comma , - 读一个逗号
  • %100[^,] - read maximum number of 100 characters that are not a comma. %100[^,] - 读取最多 100 个不是逗号的字符。 Maximum up to 101 bytes (100 characters + null byte) are stored in destination buffer.目标缓冲区中最多存储 101 个字节(100 个字符 + 空字节)。
    • %[set] - reads characters in the set %[set] - 读取集合中的字符
    • %[^set] - reads characters that are not in the set %[^set] - 读取不在集合中的字符
  • %*c - ignore one character (a comma, because %100[^,] reads up until a comma, or the string has ended, which would make scanf return here). %*c - 忽略一个字符(逗号,因为%100[^,]一直读到逗号,或者字符串结束,这将使scanf返回此处)。 Note - ignoring the result of conversion with * makes scanf not increment the return value in the case reading was successful.注意 - 使用*忽略转换结果会使scanf在读取成功的情况下不会增加返回值。
  • %f - ignore any whitespace characters, read a float (in any format - decimal, scientific or hexadecimal) %f - 忽略任何空白字符,读取浮点数(任何格式 - 十进制、科学或十六进制)

scanf("\\n%99s", destinantion); scanf("\\n%99s", 目的地);

  • \\n - read (and ignore) any number of whitespace characters (whitespace, means anything for that isspace() returns nonzero, so either space, form feed, line feed, carriage return, tab or vertical tab) \\n - 读取(并忽略)任意数量的空白字符(空白,表示任何内容isspace()返回非零值,因此空格、换页、换行、回车、制表符或垂直制表符)
  • %99s - ignore any leading whitespace characters ( \\n in front of it is useless...), then read up to 99 characters that are not whitespaces (the resulting buffer has to be at least 100 bytes long). %99s - 忽略任何前导空白字符(它前面的\\n是无用的...),然后读取最多 99 个不是空白的字符(结果缓冲区必须至少有 100 个字节长)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM