而有关问题(!EOF) [英] Question about while(!EOF)

查看:151
本文介绍了而有关问题(!EOF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯在读值从标准输入,我想直到我已经完成了阅读它一路继续读文件,所以我用

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(...) }

不过,code片段似乎并没有做任何事情,

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

编辑:我固定它

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

!的feof(标准输入)可能是没有必要的。

推荐答案

EOF 只是一个整型常量。在大多数系统中, 1 ! - 1 而(假)韩元'吨做任何事情。

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

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

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

这篇关于而有关问题(!EOF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆