用C检测EOF [英] Detecting EOF in C

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

问题描述

我使用下面的C code,直到EOF时所采取的从用户的输入,但问题是这样的code未工作时,先取后输入终止。谁能告诉我什么是错这个code。先谢谢了。

 浮动输入;的printf(输入号码:);
scanf函数(%F,&安培;输入);而(!EOF)
{
    的printf(输出:%F,输入);
    的printf(输入号码:);
    scanf函数(%F,&安培;输入);
}


解决方案

EOF 仅仅是一个值(通常是-1)宏。你必须要测试的 EOF 的东西,比如)一个的getchar的结果(电话。

一来测试流的末尾方法是使用的feof 功能。

 如果(的feof(标准输入))

请注意,该州流的末尾只会将 读取失败后。

在您的例子,你或许应该检查scanf函数的返回值,如果这个指示没有场进行读取,然后检查档案结尾。

I am using the following C code to take input from user until EOF occurs, but problem is this code is not working, it terminates after taking first input. Can anyone tell me whats wrong with this code. Thanks in advance.

float input;

printf("Input No: ");
scanf("%f", &input);

while(!EOF)
{
    printf("Output: %f", input);
    printf("Input No: ");
    scanf("%f", &input);
}

解决方案

EOF is just a macro with a value (usually -1). You have to test something against EOF, such as the result of a getchar() call.

One way to test for the end of a stream is with the feof function.

if (feof(stdin))

Note, that the 'end of stream' state will only be set after a failed read.

In your example you should probably check the return value of scanf and if this indicates that no fields were read, then check for end-of-file.

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

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