c - 关于 while(!EOF) 的问题

标签 c stdin eof

我从 stdin 读取值,我想继续读取文件,直到我完全读取它,所以我正在使用

while(!EOF){ scanf(...) }

然而,代码片段似乎并没有做任何事情,

while(!EOF){


    scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth);

    printf("---imageWidth=%d imageHeight=%d safeRegionStart=%d safeRegionWidth=%d---\n", imageWidth, imageHeight, safeRegionStart, safeRegionWidth);
    totalP = imageWidth * imageHeight ;
    totalSafeP = imageHeight * safeRegionWidth;


    printf("---total # of pixels: %d Total # of safe Pixels: %d---\n\n", totalP, totalSafeP);

    i=1;

    while(i!=totalP)
    {
        i++;
        scanf("%d", &pixel);
        printf("\nValue of pixel %d", pixel);


    }//End for scanning all pixels*/
}//while loop

编辑:我修好了

while(scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth)==4&&!feof(stdin)) { }

!feof(stdin) 可能不是必需的。

最佳答案

EOF 只是一个整数常量。在大多数系统上,它是 -1!-1false 并且 while(false) 不会执行任何操作。

您想要的是检查scanf 的返回值。 scanf 返回成功读取的项目数,最终返回 EOF

关于c - 关于 while(!EOF) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6275558/

相关文章:

c++ - 这段代码是否正确?

c - 如何在 C 中将 stdin 的值存储到数组中?

c - 通过 EOF 中断 while 循环

java - 如何在 java 中查找字符串中的 EOF?

java - EOF FOUND '}' Check style 是什么意思?

c++ - 使用 C++ 测试对原始数据类型同步锁的需求

c - 嵌套C程序

c - printf 是否将前两个赋值作为输入

java - 弗洛伊德三角的意外显示

c - 在管道之后恢复标准输入