首页 > 解决方案 > scanf( ) != EOF,脚本仍在等待进一步的输入

问题描述

一旦捕获了所有分隔的空间,目前就卡住了试图打破 for 循环。我已经在各种页面上花费了一个多小时,但我的代码看起来与我在其他文章中找到的相同。scanf() != EOF 在我的代码中永远不会发生。

int readInput(unsigned char input[], size_t inputMaxLength)
{
    int size = 0; // We want to return the length of the string

    for ( int i = 0; i < inputMaxLength ; i++) { // We only want to capture n amount of chars
        if (scanf("%hhu", (input + i)) != EOF) { // input + i to iterate through the input array
            printf("i = %d , Read %d\n", i , *(input + i)); 
            size++;
        } else {
            return size; // this never occurs which means scanf never == EOF
        }
    }
    return size;
}

输出看起来像这样,

12 65 98 45 44
i = 0 , Read 12
i = 1 , Read 65
i = 2 , Read 98
i = 3 , Read 45
i = 4 , Read 44

不幸的是,这个函数永远不会返回大小,脚本等待进一步的输入。此外,我必须使用 scanf 来捕获此函数的输入是一个约束。

标签: cscanfeof

解决方案


如果您从文件中获取输入,那么我猜它应该可以正常工作,但是如果您从终端获取输入,那么在输入带有空格(如15 20 39)的数字后,您按回车键,这是一个换行符\n而不是 EOF。

对于终端,您必须遵循这样的逻辑

  • 由于最后一件事总是<char><newline>如此,所以如果 scanf like scanf("%hhu%c", (input + i), &temp)
  • 然后我们可以标记一个结束条件,如
if (temp == '\n')
{
    // add the last character in the array then break
}

我按照上面说的逻辑,在onlinegdb.com上测试了代码(有点多余,以后可以清理一下)

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include<malloc.h>

int readInput(unsigned char input[], size_t inputMaxLength)
{
    int size = 0; // We want to return the length of the string

    for ( int i = 0; i < inputMaxLength; i++) { // We only want to capture n amount of chars
        char c, d;
        scanf("%hhu%c", &c, &d);
        if (d != '\n') { // input + i to iterate through the input array
            *(input + i) = c;
            printf("i = %d , Read %d\n", i , *(input + i)); 
            size++;
        } else {
            *(input + i) = c;
            printf("i = %d , Read %d\n", i , *(input + i)); 
            size++;
            printf("Returning size\n");
            return size; // this never occurs which means scanf never == EOF
        }
    }
    return size;
}

int main()
{
    size_t a = 5;
    unsigned char *input = (char *)malloc(a*sizeof(char));
    printf("%d", readInput(input, a));
    return 0;
}

这是我的测试用例输出

12 34 5                                                                                                                                                                       
i = 0 , Read 12                                                                                                                                                               
i = 1 , Read 34                                                                                                                                                               
i = 2 , Read 5                                                                                                                                                                
Returning size                                                                                                                                                                
3

推荐阅读