简体   繁体   English

为什么 sscanf 读取的内容超出预期?

[英]Why does sscanf read more than expected?

sscanf supports %n to count how many bytes are read. sscanf支持%n计算读取的字节数。

Why does sscanf sometimes read additional bytes?为什么 sscanf 有时会读取额外的字节?

#include <stdio.h>

int main()
{
    char *data = "X\n \n\x09\n \x10\n";
    int len = 0;

    sscanf(data, "X%n", &len);
    printf("%i\n", len);

    sscanf(data, "X\n%n", &len);
    printf("%i\n", len);

    return 0;
}

This program prints:该程序打印:

1
7

I would expect:我希望:

1
2

(1 for X and 2 for X\n .) (1 代表X和 2 代表X\n 。)

Why does it read more bytes than expected?为什么它读取的字节数比预期的多?

From cppreference :cppreference

The format string consists of格式字符串包括

  • non-whitespace multibyte characters except %: each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal.除 % 以外的非空白多字节字符:格式字符串中的每个此类字符都使用来自输入 stream 的完全相同的字符,或者如果 ZF7B44CFAFD5C52223D54988E 上的下一个字符不相等,则导致 function 失败。

  • whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling isspace in a loop).空白字符:格式字符串中的任何单个空白字符都会消耗输入中所有可用的连续空白字符(就像在循环中调用 isspace 一样确定)。 Note that there is no difference between "\n", " ", "\t\t", or other whitespace in the format string.请注意,格式字符串中的“\n”、“”、“\t\t”或其他空格之间没有区别。

Thus, your \n in the second format string will cause the function to consume all remaining whitespace characters – which is actually all 6 characters following the X and preceding the 0x10 .因此,第二个格式字符串中的\n将导致 function 消耗所有剩余的空白字符——这实际上是X之后和0x10之前的所有 6 个字符。

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

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