检测 C 中的 EOF

发布于 2024-08-04 15:33:50 字数 269 浏览 4 评论 0原文

我正在使用以下 C 代码从用户处获取输入,直到 EOF 发生,但问题是此代码不起作用,它在获取第一个输入后终止。这段代码有什么问题?

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

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. What's wrong with this code?

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

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

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

发布评论

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

评论(6

花开柳相依 2024-08-11 15:33:50

EOF 只是一个带有值(通常为-1)的宏。您必须针对 EOF 进行测试,例如 getchar() 调用的结果。

测试流结束的一种方法是使用 feof 函数。

if (feof(stdin))

请注意,“流结束”状态只会在读取失败之后设置。

在您的示例中,您可能应该检查 scanf 的返回值,如果这表明没有读取任何字段,则检查文件结尾。

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.

葬シ愛 2024-08-11 15:33:50

EOF 是 C 中的常量。您没有检查实际文件中的 EOF。你需要做这样的事情

while(!feof(stdin))

这里是feof的文档。您还可以检查 scanf 的返回值。它返回成功转换的项目数,如果到达文件末尾,则返回 EOF。

EOF is a constant in C. You are not checking the actual file for EOF. You need to do something like this

while(!feof(stdin))

Here is the documentation to feof. You can also check the return value of scanf. It returns the number of successfully converted items, or EOF if it reaches the end of the file.

孤独难免 2024-08-11 15:33:50

另一个问题是您仅使用 scanf("%f", &input); 进行阅读。如果用户输入无法解释为 C 浮点数的内容(例如“pi”),则 scanf() 调用不会向 input 分配任何内容,并且不会从那里进步。这意味着它将尝试继续读取“pi”,但会失败。

鉴于其他发帖者正确推荐的对 while(!feof(stdin)) 的更改,如果您在其中输入“pi”,则会出现无限循环打印出输入的前一个值 并打印提示,但程序永远不会处理任何新输入。

scanf() 返回它对输入变量进行的赋值次数。如果它没有进行赋值,则意味着它没有找到浮点数,您应该使用诸如 char string[100];scanf("%99s", string); 之类的内容来阅读更多输入代码>.这将从输入流中删除下一个字符串(无论如何,最多 99 个字符 - 额外的 char 用于字符串上的空终止符)。

你知道,这让我想起了我讨厌 scanf() 的所有原因,以及为什么我使用 fgets() 而不是,然后可能使用 sscanf( )

Another issue is that you're reading with scanf("%f", &input); only. If the user types something that can't be interpreted as a C floating-point number, like "pi", the scanf() call will not assign anything to input, and won't progress from there. This means it would attempt to keep reading "pi", and failing.

Given the change to while(!feof(stdin)) which other posters are correctly recommending, if you typed "pi" in there would be an endless loop of printing out the former value of input and printing the prompt, but the program would never process any new input.

scanf() returns the number of assignments to input variables it made. If it made no assignment, that means it didn't find a floating-point number, and you should read through more input with something like char string[100];scanf("%99s", string);. This will remove the next string from the input stream (up to 99 characters, anyway - the extra char is for the null terminator on the string).

You know, this is reminding me of all the reasons I hate scanf(), and why I use fgets() instead and then maybe parse it using sscanf().

陪我终i 2024-08-11 15:33:50

作为起点,你可以尝试替换

while(!EOF)

while(!feof(stdin))

as a starting point you could try replacing

while(!EOF)

with

while(!feof(stdin))
仄言 2024-08-11 15:33:50

您想要检查 scanf() 的结果以确保转换成功;如果没有,则以下三件事之一是正确的:

  1. scanf() 因对 %f 转换说明符无效的字符而阻塞(即不是数字、点、'e'、或“E”);
  2. scanf() 已检测到 EOF;
  3. scanf() 在读取 stdin 时检测到错误。

例子:

int moreData = 1;
...
printf("Input no: ");
fflush(stdout);
/**
 * Loop while moreData is true
 */
while (moreData)
{
  errno = 0;
  int itemsRead = scanf("%f", &input);
  if (itemsRead == 1)
  {
    printf("Output: %f\n", input);
    printf("Input no: ");
    fflush(stdout);
  }
  else
  {
    if (feof(stdin))
    {
      printf("Hit EOF on stdin; exiting\n");
      moreData = 0;
    }
    else if (ferror(stdin))
    {
      /**
       * I *think* scanf() sets errno; if not, replace
       * the line below with a regular printf() and
       * a generic "read error" message.
       */
      perror("error during read");
      moreData = 0;
    }
    else
    {
      printf("Bad character stuck in input stream; clearing to end of line\n");
      while (getchar() != '\n')
        ; /* empty loop */
      printf("Input no: ");
      fflush(stdout);
    }
 }

You want to check the result of scanf() to make sure there was a successful conversion; if there wasn't, then one of three things is true:

  1. scanf() is choking on a character that isn't valid for the %f conversion specifier (i.e., something that isn't a digit, dot, 'e', or 'E');
  2. scanf() has detected EOF;
  3. scanf() has detected an error on reading stdin.

Example:

int moreData = 1;
...
printf("Input no: ");
fflush(stdout);
/**
 * Loop while moreData is true
 */
while (moreData)
{
  errno = 0;
  int itemsRead = scanf("%f", &input);
  if (itemsRead == 1)
  {
    printf("Output: %f\n", input);
    printf("Input no: ");
    fflush(stdout);
  }
  else
  {
    if (feof(stdin))
    {
      printf("Hit EOF on stdin; exiting\n");
      moreData = 0;
    }
    else if (ferror(stdin))
    {
      /**
       * I *think* scanf() sets errno; if not, replace
       * the line below with a regular printf() and
       * a generic "read error" message.
       */
      perror("error during read");
      moreData = 0;
    }
    else
    {
      printf("Bad character stuck in input stream; clearing to end of line\n");
      while (getchar() != '\n')
        ; /* empty loop */
      printf("Input no: ");
      fflush(stdout);
    }
 }
旧人 2024-08-11 15:33:50
while(scanf("%d %d",a,b)!=EOF)
{

//do .....
}
while(scanf("%d %d",a,b)!=EOF)
{

//do .....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文