关于 while(!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)
可能没有必要。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
EOF
只是一个整数常量。在大多数系统上它是-1
。!-1
是false
并且while(false)
不会做任何事情。您想要的是检查
scanf
的返回值。scanf
返回成功读取的项目数,并最终返回EOF
。EOF
is only an integer constant. On most systems it is-1
.!-1
isfalse
andwhile(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 eventuallyEOF
.嗯,这很容易回答:
EOF 是一个常量
#define
,例如#define EOF -1
。因此,您的
while(!EOF)
条件将始终为 false,并且循环不会执行。您需要根据 EOF 检查scanf
的返回值。你需要类似的东西:
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 ofscanf
against EOF.You need something like:
您必须使用变量来保存可能为 EOF 的 char 值。类似 ..
否则 !EOF 总是错误的。
You have to use a variable to hold a char value that can be potentially
EOF
. Something like ..Otherwise !EOF is always false.
永远不会进入循环。
EOF
是一个常量值,即-1
(请检查stdio.h
来了解此定义)。因此!EOF
是0
,这是 false,因此永远不会被输入。要检查文件是否已结束,可以使用:
if (feof (file_ptr)) break;
The loop will never be entered.
EOF
is a constant value which is-1
(checkstdio.h
for this definition). So!EOF
is0
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;