C语言中的EOF问题 [英] ProbIem with EOF in C

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

问题描述

我正在编写一个程序,该程序应该读取两个可以包含换行符和其他各种字符的字符串.因此,我使用EOF(Ctrl-Z或Ctrl-D)结束字符串.

这对于第一个变量可以很好地工作,但是对于第二个变量却可以,但是这似乎是有问题的,因为显然有些东西卡在了输入缓冲区中,并且用户没有输入任何内容.

我尝试使用while (getchar() != '\n');和几个类似的变体来清理缓冲区,但是似乎没有帮助.所有清除尝试都导致一个无限循环,并且如果不进行清除,则不可能添加第二个变量.

两个变量的字符在这样的循环中读取:while((c = getchar()) != EOF),这表明我卡在缓冲区中的是EOF.还是以其他方式影响程序的行为?我使用的逻辑有问题吗?

苦苦挣扎几个小时后,我开始感到绝望了.

代码:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int x = 0;
    int c;
    char a[100];
    char b[100];

    printf("Enter a: ");
    while((c = getchar()) != EOF)
    {
        a[x] = c;
        x++;
    }
    a[x] = '\0';
    x = 0;

    /*while (getchar() != '\n'); - the non-working loop*/

    printf("\nEnter b: ");
    while((c = getchar()) != EOF)
    {
        b[x] = c;
        x++;
    }
    b[x] = '\0';

    printf("\n\nResults:\na: %s\n", a);
    printf("b: %s\n", b);

    return(0);
}

解决方案

从终端收到EOF之后,您将不会再收到任何其他数据.无法取消对输入的EOF操作-文件的末尾就是末尾.

因此,您应该定义每个变量都在单独的行上输入,并让用户按Enter键而不是EOF.您仍然需要检查是否收到eof,因为这意味着用户实际键入了EOF,并且您将看不到其他任何内容-在这种情况下,您需要跳出循环并打印错误消息.

I'm writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I'm using EOF (Ctrl-Z or Ctrl-D) to end the string.

This works fine with the first variable, but with the second variable, however, this seems to be problematic as apparently something is stuck in the input buffer and the user doesn't get to type in anything.

I tried to clean the buffer with while (getchar() != '\n'); and several similar variations but nothing seems to help. All cleaning attempts have resulted in an infinite loop, and without cleaning, adding the second variable is impossible.

The characters for both of the variables are read in a loop like this: while((c = getchar()) != EOF), which would suggest it is EOF what I have stuck in my buffer. Or does it affect the behavior of the program in some other way? Is there something wrong with the logic I'm using?

I'm starting to get bit desperate after struggling with this for hours.

code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int x = 0;
    int c;
    char a[100];
    char b[100];

    printf("Enter a: ");
    while((c = getchar()) != EOF)
    {
        a[x] = c;
        x++;
    }
    a[x] = '\0';
    x = 0;

    /*while (getchar() != '\n'); - the non-working loop*/

    printf("\nEnter b: ");
    while((c = getchar()) != EOF)
    {
        b[x] = c;
        x++;
    }
    b[x] = '\0';

    printf("\n\nResults:\na: %s\n", a);
    printf("b: %s\n", b);

    return(0);
}

解决方案

After you received an EOF from the terminal, you will not receive any additional data. There is no way of un-EOF-ing the input - the end of the file is, well, the end.

So you should define that each variable is input on a separate line, and have users press enter instead of EOF. You still need to check whether you have received eof, because that means that the user actually typed EOF, and you won't see anything else - in this case, you need to break out of the loop and print an error message.

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

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