关于 while(!EOF) 的问题

发布于 2024-11-14 13:06:19 字数 1019 浏览 2 评论 0原文

我从 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) 可能没有必要。

Im reading in values from stdin and I want to keep reading the file until I have completed reading it all the way, so I am using

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

However, the code fragment doesn't seem to do anything,

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

EDIT: I fixed it

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

!feof(stdin) probably isn't necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

当梦初醒 2024-11-21 13:06:19

EOF 只是一个整数常量。在大多数系统上它是-1!-1false 并且 while(false) 不会做任何事情。

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

EOF is only an integer constant. On most systems it is -1. !-1 is false and while(false) won't do anything.

What you want is to check the return values of scanf. scanf returns the number of successfully read items and eventually EOF.

二货你真萌 2024-11-21 13:06:19

嗯,这很容易回答:

EOF 是一个常量#define,例如#define EOF -1

因此,您的 while(!EOF) 条件将始终为 false,并且循环不会执行。您需要根据 EOF 检查 scanf 的返回值。

你需要类似的东西:

while(scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth) != EOF){

Well, this is easy to answer:

EOF is a constant #define, e.g. #define EOF -1.

So your while(!EOF) condition will always be false and the loop won't execute. You need to check the return value of scanf against EOF.

You need something like:

while(scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth) != EOF){
¢蛋碎的人ぎ生 2024-11-21 13:06:19

您必须使用变量来保存可能为 EOF 的 char 值。类似 ..

while(4==scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth)) {
//do stuff
}

否则 !EOF 总是错误的。

You have to use a variable to hold a char value that can be potentially EOF. Something like ..

while(4==scanf("%d %d %d %d", &imageWidth, &imageHeight, &safeRegionStart, &safeRegionWidth)) {
//do stuff
}

Otherwise !EOF is always false.

め可乐爱微笑 2024-11-21 13:06:19

永远不会进入循环。 EOF 是一个常量值,即 -1(请检查 stdio.h 来了解此定义)。因此 !EOF0,这是 false,因此永远不会被输入。

要检查文件是否已结束,可以使用: if (feof (file_ptr)) break;

while (1)
{
   /* Read file */
   if (feof (file_ptr))
     break;
   /* Do work */
}

The loop will never be entered. EOF is a constant value which is -1 (check stdio.h for this definition). So !EOF is 0 which is false, so it will never be entered.

To check that if the file has ended or not you can use: if (feof (file_ptr)) break;

while (1)
{
   /* Read file */
   if (feof (file_ptr))
     break;
   /* Do work */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文