问题在C EOF [英] Problem with EOF in C

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

问题描述

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

此正常工作与第一可变,但与第二个变量,然而,这似乎明显的东西是停留在输入缓冲器和用户未得到任何输入到有问题的。

我试图清理与而(getchar函数()='\\ n'!)缓冲区; 和几个类似的变化,但似乎没有任何帮助。所有清洗的尝试已导致一个无限循环,并且没有清洗,加入第二变量是不可能的。

这两个变量的人物在这样的循环中读出:(!(C =的getchar())= EOF),而,这将表明它是EOF我已经停留在我的缓冲区。或者,它会影响其他方式的程序的行为?是不是有什么毛病我用什么逻辑?

我开始这个挣扎小时后得到位绝望了。

code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
    INT X = 0;
    INT℃;
    所以char a [100];
    炭B〔100〕;    的printf(请输入:);
    而((C =的getchar())!= EOF)
    {
    一个[X] = C;
    X ++;
    }
    一个[X] ='\\ 0';
    X = 0;    / *一段时间(的getchar()='\\ n'!); - 非工作循环* /    的printf(\\ n输入B:);
    而((C =的getchar())!= EOF)
    {
    B〔X] = C;
    X ++;
    }
    B〔X] ='\\ 0';    的printf(\\ n \\ nResults:\\钠:%S \\ n,一);
    的printf(B:%S \\ n,B);    返回(0);
}

动态内存问题:

我的程序也应该处理字符串超过100个字符。起初,我打算通过动态内存分配,以解决这个问题,但是当我有与上述无限循环和内存相关的崩溃问题,我离开了出来,并切换到char [100]。

我想我想一般是这样的:

 ,而((C =的getchar())!= EOF)
{
  A =的malloc(sizeof的(炭));
  一个[X] = C;
  X ++;
}

那是可能的(或合理)的方式做到这一点?我想该是正在处理存在的每一个字符分配更多的内存。独立。随着code一样,(此例中可能含有语法错误)我遇到故障,所以看起来我的malloc可能不在这里是正确的功能,或者我想错了。假如它甚至有可能。


解决方案

在从终端接收到一个EOF后,您将不会收到任何额外的数据即可。有没有非EOF-ING输入方式 - 在文件的结尾是,好了,结束

所以,你应该定义每个变量是在一个单独的线路输入,并让用户preSS输入,而不是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.

[edit: added code below]

[edit 2: clearerr() seems to make this EOF solution work after all.

It seems to run in its original form like I intended under Linux, I was trying it with Windows yesterday.]

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);
}

[edit 3:]

Dynamic memory issue:

My program is also supposed to handle strings longer than 100 characters. Originally I intended to solve that by dynamic memory allocation, but when I had problems with the infinite loop described above and memory-related crashes I left it out and switched over to char[100].

I think what I tried was generally something like this:

while((c = getchar()) != EOF)
{
  a = malloc(sizeof(char));
  a[x] = c;
  x++;
}

Is that a possible (or sensible) way to do that? I'm trying to allocate more memory for every character that's being handled there. Individually. With code like that (this example contains probably syntax errors) I experienced crashes, so looks to me malloc might not be the right function here, or I'm trying it wrong. Supposing it's even possible.

解决方案

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天全站免登陆