简体   繁体   English

如何使用scanf扫描多个单词?

[英]How to use scanf to scan for multiple words?

I am trying to work on a function that take the input, the minimum character limit and the maximum character limit.我正在尝试处理接受输入、最小字符限制和最大字符限制的 function。 I need to accept the input and count the letters even if there are 2 words or more, and I saw people said scanf("%30[^\n]%*c") would do the trick.即使有 2 个或更多单词,我也需要接受输入并计算字母,我看到人们说scanf("%30[^\n]%*c")可以解决问题。 However, this only work if it is the first input ever, nothing more.但是,这仅在它是第一个输入时才有效,仅此而已。 If there has been any input above, it would just terminate the line, leaving count as zero, and run the loop infinitely.如果上面有任何输入,它将终止该行,将 count 保留为零,并无限运行循环。 Anyone knows why?有谁知道为什么?

NOTE: I can not use anything from the <string.h> header file.注意:我不能使用 <string.h> header 文件中的任何内容。

{
    int n = 1;
    
    while (n == 1)
    {
        int count = 0;
        scanf("%30[^\n]%*c", input);
        while (input[count] != '\0')
        {
            count++;
        }
        if (minimum == maximum)
        {
            if (count > maximum)
            {
                printf("String length must be exactly %d chars: ", minimum);
            }
            else if (count == minimum)
            {
                n = 0;
                return input;
            }
            else if (count < minimum)
            {
                printf("String length must be exactly %d chars: ", minimum);
            }
        }
        else 
        {
            if (count > maximum)
            {
                printf("String length must be no more than %d chars: ", maximum);
            }
            else if (minimum <= count && count <= maximum)
            {
                n = 0;
                return input;
            }
            else if (count < minimum)
            {
                printf("String length must be between %d and %d chars: ", minimum, maximum);
            }
        }
    }
}

The problem with scanf("%30[^\n]%*c", input); scanf("%30[^\n]%*c", input);的问题is it fails if the new byte from stdin is a newline, returning 0 and leaving input unchanged, potentially uninitialized, causing the rest of the code to have undefined behavior.如果来自stdin的新字节是换行符,它是否会失败,返回0并保持input不变,可能未初始化,导致代码的 rest 具有未定义的行为。

Also note that this format will cause scanf() to read and discard the byte pending after the conversion, either the newline, which is your intent or whatever 31st character was typed by the user on the input line.另请注意,此格式将导致scanf()在转换后读取并丢弃待处理的字节,无论是换行符,这是您的意图,还是用户在输入行上键入的任何第 31 个字符。

You should test the return value of scanf() to detect a conversion error and/or the end of file and only scan the array if scanf() returns 1 .您应该测试scanf()的返回值以检测转换错误和/或文件结尾,并且仅在scanf()返回1时扫描数组。

You should discard extra bytes at the end of the line with a loop:您应该在行尾使用循环丢弃多余的字节:

int c;
while ((c = getchar()) != EOF && c != '\n')
    continue;

Or with scanf() :或使用scanf()

scanf("%*[^\n]");  // discard the rest of the input line if any
scanf("%*c");      // discard the newline if any.

To get rid of the pending newline left from previous calls to scanf() , you can write:要摆脱先前调用scanf()留下的挂起换行符,您可以编写:

int c;
if ((c = getchar()) != '\n')
    ungetc(c, stdin);

Or using scanf :或使用scanf

scanf("%1*[\n]");  // read and discard at most 1 newline byte

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

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