ProbIem with EOF in C

与世无争的帅哥 提交于 2019-11-30 10:14:42

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.

EOF isn't a character - it's a special value that the input functions return to indicate a condition, that the "end of file" on that input stream has been reached. As Martin v. Löwis says, once that "end of file" condition occurs, it means that no more input will be available on that stream.

The confusion arises because:

  • Many terminal types recognize a special keystroke to signal "end of file" when the "file" is an interactive terminal (eg. Ctrl-Z or Ctrl-D); and
  • The EOF value is one of the values that can be returned by the getchar() family of functions.

You will need to use an actual character value to separate the inputs - the ASCII nul character '\0' might be a good choice, if that can't appear as a valid value within the inputs themselves.

I run the code on my linux box, here is the result:

Enter a: qwer
asdf<Ctrl-D><Ctrl-D>
Enter b: 123
456<Ctrl-D><Ctrl-D>

Results:
a: qwer
asdf
b: 123
456

Two Ctrl-D was needed because the terminal input buffer was not empty.

You could use the null character ('\0') to separate the variables. Various UNIX tools (e.g. find) are capable of separating their output items in this way, which would suggest that it's a fairly standard method.

Another advantage of this is that you can read the stream into a single buffer and then create an array of char*s to point to the individual strings, and each string will be correctly '\0'-terminated without you having to change anything in the buffer manually. This means less memory allocation overhead, which may make your program run noticeably faster depending on how many variables you're reading. Of course, this is only necessary if you need to hold all the variables in memory at the same time — if you're dealing with them one-at-a-time, you don't get this particular advantage.

What you are trying is fundamentally impossible with EOF.

Although it behaves like one in some ways, EOF is not a character in the stream but an environment-defined macro representing the end of the stream. I haven't seen your code, but I gather you're doing is something like this:

while ((c=getchar()) != EOF) {
    // do something
}
while ((c=getchar()) != EOF) {
    // do something else
}

When you type the EOF character the first time, to end the first string, the stream is irrevocably closed. That is, the status of the stream is that it is closed.

Thus, the contents of the second while loop are never run.

Rather than stopping reading input at EOF -- which isn't a character -- stop at ENTER.

while((c = getchar()) != '\n')
{
    if (c == EOF) /* oops, something wrong, input terminated too soon! */;
    a[x] = c;
    x++;
}

EOF is a signal that the input terminated. You're almost guaranteed that all inputs from the user end with '\n': that's the last key the user types!!!


Edit: you can still use Ctrl-D and clearerr() to reset the input stream.

#include <stdio.h>

int main(void) {
  char a[100], b[100];
  int c, k;

  printf("Enter a: "); fflush(stdout);
  k = 0;
  while ((k < 100) && ((c = getchar()) != EOF)) {
    a[k++] = c;
  }
  a[k] = 0;

  clearerr(stdin);

  printf("Enter b: "); fflush(stdout);
  k = 0;
  while ((k < 100) && ((c = getchar()) != EOF)) {
    b[k++] = c;
  }
  b[k] = 0;

  printf("a is [%s]; b is [%s]\n", a, b);
  return 0;
}
$ ./a.out
Enter a: two
lines (Ctrl+D right after the next ENTER)
Enter b: three
lines
now (ENTER + Ctrl+D)
a is [two
lines (Ctrl+D right after the next ENTER)
]; b is [three
lines
now (ENTER + Ctrl+D)
]
$

How do you enter null in the program?

You can implement the -print0 function using:

putchar(0);

This will print an ASCII nul character '\0' to sdtout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!