C 程序不会从 STDIN 读取输入

C Program won't read input from STDIN

我正在编写一个基本的统计程序,这是我第一个用纯 C 语言编写的程序,但我一生都无法解决这个问题。当从命令行手动获取输入时,它工作得很好。但是,当从输入文件中输入这些数字时,它不会读取其中的任何一个。这是源代码:

statistics.c:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[]){

    // Create global variables, introduce program
    int minimum = INT_MAX;
    int maximum = INT_MIN;
    int i = 0;
    int count = 0;
    double total = 0.0;

    printf("%s\n", "Program1");
    printf("%s\n", "Enter nums (0 terminates):");

    scanf("%d", &i);    // Scan in number   
    while (i!=0)
    {
        printf("%d\n", i);          // Print the number just entered
        count++;                    // Increment counter
        total += i;                 // Add to total
        if (i > max) {max = i;}     // Check for maximum
        if (i < min) {min = i;}     // Check for minimum
        scanf("%d", &i);            // Read in the next number
    }
    printf("%s%d\n", "Nums entered: ", counter);
    printf("%s%d%s%d\n", "range: ", min, ", ", max);
    printf("%s%f\n", "mean: ", total/counter);
    return EXIT_SUCCESS;
}

input.txt:

2 3 5 0

当我在终端中 运行 ./program 并手动输入这些数字时,它会给出预期的输出。但是当我 运行 ./program < input.txt 时,什么也没有发生,它卡住了,所以我不得不使用 ^C 来终止进程。有什么想法吗??

原始代码post定义了变量minimummaximumcount并使用了变量minmaxcounter 分别。由于原始代码因此无法编译,我们只能确定您的 运行ning 代码不是从最初显示的源代码创建的。 不要 post 给您带来麻烦的代码的近似值 — 确保您的代码 post导致您描述的问题(它编译;它 运行s;它产生声称的输出,至少在您的机器上)。

这是代码的拼写更正版本:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int min = INT_MAX;
    int max = INT_MIN;
    int i = 0;
    int count = 0;
    double total = 0.0;

    printf("%s\n", "Program1");
    printf("%s\n", "Enter nums (0 terminates):");

    scanf("%d", &i);
    while (i!=0)
    {
        printf("%d\n", i);
        count++;
        total += i;
        if (i > max) {max = i;}
        if (i < min) {min = i;}
        scanf("%d", &i);
    }
    printf("%s%d\n", "Nums entered: ", count);
    printf("%s%d%s%d\n", "range: ", min, ", ", max);
    printf("%s%f\n", "mean: ", total/count);
    return EXIT_SUCCESS;
}

当 运行 文件 input.txt 包含:

2 3 5 0

它生成输出:

Program1
Enter nums (0 terminates):
2
3
5
Nums entered: 3
range: 2, 5
mean: 3.333333

因此,我无法重现您声称的问题,但这可能是因为我看不到您的真实代码,或者可能不是您的真实数据。如果我从文件中省略 0,那么我会得到一个无限循环,每次都会打印 5

这是一个具有更强大输入处理能力的替代版本;它检查 scanf() 的 return 值并避免重复调用。

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int min = INT_MAX;
    int max = INT_MIN;
    int i = 0;
    int count = 0;
    double total = 0.0;

    printf("%s\n", "Program1");
    printf("%s\n", "Enter nums (0 terminates):");

    while (scanf("%d", &i) == 1 && i != 0)
    {
        printf("%d\n", i);
        count++;
        total += i;
        if (i > max)
            max = i;
        if (i < min)
            min = i;
    }

    printf("Nums entered: %d\n", count);
    printf("Range: %d to %d\n", min, max);
    printf("Mean: %f\n", total / count);
    return EXIT_SUCCESS;
}

此代码在没有 0 作为最后一个数字的数据文件上正常工作。