简体   繁体   English

C 编程语言中的 EOF 是什么?

[英]What is EOF in the C programming language?

How do you get to see the last print?你怎么才能看到最后的印刷品? In other words what to put in for EOF?换句话说,要为 EOF 输入什么? I checked the definitions and it says EOF is -1.我检查了定义,它说EOF是-1。

And if you enter Ctrl-D you won't see anything.如果您输入 Ctrl-D,您将什么也看不到。

#include <stdio.h>

int main() {
 int c;
 while((c = getchar() != EOF)) {
  printf("%d\n", c);
 }
 printf("%d - at EOF\n", c);
}

On Linux systems and OS X, the character to input to cause an EOF is Ctrl - D . 在Linux系统和OS X上,要引起EOF的输入字符是Ctrl - D For Windows, it's Ctrl - Z . 对于Windows,是Ctrl - Z

Depending on the operating system, this character will only work if it's the first character on a line, ie the first character after an Enter . 根据不同的操作系统,只有当它是一行中的第一个字符(即Enter之后的第一个字符)时,此字符才起作用。 Since console input is often line-oriented, the system may also not recognize the EOF character until after you've followed it up with an Enter . 由于控制台输入通常是面向行的,因此系统可能无法识别EOF字符,直到您按Enter键为止。

And yes, if that character is recognized as an EOF, then your program will never see the actual character. 是的,如果该字符被识别为EOF,则您的程序将永远看不到实际的字符。 Instead, a C program will get a -1 from getchar() . 相反,C程序将从getchar()获得-1

You should change your parenthesis to 您应该将括号更改为

while((c = getchar()) != EOF)

Because the "=" operator has a lower precedence than the "!=" operator. 因为“ =”运算符的优先级低于“!=”运算符。 Then you will get the expected results. 然后您将获得预期的结果。 Your expression is equal to 你的表情等于

while (c = (getchar()!= EOF))

You are getting the two 1's as output, because you are making the comparison "c!=EOF". 您将获得两个1,因为您正在进行比较“ c!= EOF”。 This will always become one for the character you entered and then the "\\n" that follows by hitting return. 对于输入的字符,它将始终成为一个字符,然后按回车键将成为“ \\ n”。 Except for the last comparison where c really is EOF it will give you a 0. 除了最后一个比较,其中c确实是EOF之外,它会为您提供0。

EDIT about EOF: EOF is typically -1, but this is not guaranteed by the standard. 有关EOF的编辑:EOF通常为-1,但这不是标准所保证的。 The standard only defines about EOF in section 7.19.1: 该标准仅在7.19.1节中定义了EOF:

EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream; EOF扩展为整数类型为int且为负值的整数常量表达式,由多个函数返回以指示文件结束,即不再有流输入。

It is reasonable to assume that EOF equals -1, but when using EOF you should not test against the specific value, but rather use the macro. 假设EOF等于-1是合理的,但是使用EOF时,您不应针对特定值进行测试,而应使用宏。

The value of EOF is a negative integer to distinguish it from "char" values that are in the range 0 to 255. It is typically -1, but it could be any other negative number ... according to the POSIX specs, so you should not assume it is -1. EOF的值是一个负整数,以区别于0到255范围内的“ char”值。它通常是-1,但是根据POSIX规范,它可以是任何其他负数...不应该假设它为-1。

The ^D character is what you type at a console stream on UNIX/Linux to tell it to logically end an input stream. ^ D字符是您在UNIX / Linux上的控制台流中键入的内容,用于告诉它逻辑上结束输入流。 But in other contexts (like when you are reading from a file) it is just another data character. 但是在其他情况下(例如,当您从文件读取时),它只是另一个数据字符。 Either way, the ^D character (meaning end of input) never makes it to application code. 无论哪种方式,^ D字符(表示输入的结尾)都不会进入应用程序代码。

As @Bastien says, EOF is also returned if getchar() fails. 正如@Bastien所说,如果getchar()失败,也会返回EOF。 Strictly speaking, you should call ferror or feof to see whether the EOF represents an error or an end of stream. 严格来说,应该调用ferrorfeof来查看EOF是表示错误还是流结束。 But in most cases your application will do the same thing in either case. 但是在大多数情况下,无论哪种情况,您的应用程序都会做同样的事情。

EOF means end of file. EOF表示文件结束。 It's a sign that the end of a file is reached, and that there will be no data anymore. 这表明已到达文件末尾,并且将不再有数据。

Edit: 编辑:

I stand corrected. 我站得住了。 In this case it's not an end of file. 在这种情况下,它不是文件结尾。 As mentioned, it is passed when CTRL+d (linux) or CTRL+z (windows) is passed. 如前所述,当传递CTRL + d(linux)或CTRL + z(windows)时传递。

Couple of typos: 几个错别字:

while((c = getchar())!= EOF)

in place of: 代替:

while((c = getchar() != EOF))

Also getchar() treats a return key as a valid input, so you need to buffer it too.EOF is a marker to indicate end of input. 另外getchar()将返回键视为有效输入,因此您也需要对其进行缓冲.EOF是指示输入结束的标记。 Generally it is an int with all bits set. 通常,它是一个设置了所有位的整数。


#include <stdio.h>
int main()
{
 int c;
 while((c = getchar())!= EOF)
 {
  if( getchar() == EOF )
    break;
  printf(" %d\n", c);
 }
  printf("%d %u %x- at EOF\n", c , c, c);
}

prints: 印刷品:

49
50
-1 4294967295 ffffffff- at EOF

for input: 用于输入:

1
2
<ctrl-d>

nput from a terminal never really "ends" (unless the device is disconnected), but it is useful to enter more than one "file" into a terminal, so a key sequence is reserved to indicate end of input. 来自终端的输入永远不会真正“结束”(除非断开设备连接),但是在终端中输入多个“文件”很有用,因此保留了一个键序列来指示输入结束。 In UNIX the translation of the keystroke to EOF is performed by the terminal driver, so a program does not need to distinguish terminals from other input files. 在UNIX中,将按键转换为EOF是由终端驱动程序执行的,因此程序无需将终端与其他输入文件区分开。 By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. 默认情况下,驱动程序将一行开头的Control-D字符转换为文件结尾指示符。 To insert an actual Control-D (ASCII 04) character into the input stream, the user precedes it with a "quote" command character (usually Control-V). 要将实际的Control-D(ASCII 04)字符插入输入流,用户必须在其前面加上“ quote”命令字符(通常是Control-V)。 AmigaDOS is similar but uses Control-\\ instead of Control-D. AmigaDOS与之类似,但使用Control- \\代替Control-D。

In Microsoft's DOS and Windows (and in CP/M and many DEC operating systems), reading from the terminal will never produce an EOF. 在Microsoft的DOS和Windows(以及CP / M和许多DEC操作系统)中,从终端读取将永远不会产生EOF。 Instead, programs recognize that the source is a terminal (or other "character device") and interpret a given reserved character or sequence as an end-of-file indicator; 相反,程序会识别出源是终端(或其他“字符设备”),并将给定的保留字符或序列解释为文件结束指示符。 most commonly this is an ASCII Control-Z, code 26. Some MS-DOS programs, including parts of the Microsoft MS-DOS shell (COMMAND.COM) and operating-system utility programs (such as EDLIN), treat a Control-Z in a text file as marking the end of meaningful data, and/or append a Control-Z to the end when writing a text file. 最常见的是代码为26的ASCII Control-Z。某些MS-DOS程序(包括Microsoft MS-DOS shell(COMMAND.COM)的一部分和操作系统实用程序(例如EDLIN))对待Control-Z在文本文件中标记为有意义的数据的结尾,和/或在编写文本文件时在末尾附加Control-Z。 This was done for two reasons: 这样做有两个原因:

  1. Backward compatibility with CP/M. 与CP / M的向后兼容性。 The CP/M file system only recorded the lengths of files in multiples of 128-byte "records", so by convention a Control-Z character was used to mark the end of meaningful data if it ended in the middle of a record. CP / M文件系统仅以128字节“记录”的倍数记录文件的长度,因此按照惯例,如果Control-Z字符在记录中间结束,则使用Control-Z字符标记有意义的数据的结尾。 The MS-DOS filesystem has always recorded the exact byte-length of files, so this was never necessary on MS-DOS. MS-DOS文件系统始终记录文件的确切字节长度,因此在MS-DOS上从来没有必要这样做。

  2. It allows programs to use the same code to read input from both a terminal and a text file. 它允许程序使用相同的代码从终端和文本文件读取输入。

#include <stdio.h>

int main() {
    int c;
    while((c = getchar()) != EOF) { //precedence of != is greater than =, so use braces
        printf("%d\n", c);
    }
    printf("%d - at EOF\n", c);
}

I think this is right way to check value of EOF. 我认为这是检查EOF值的正确方法。 And I checked the output. 我检查了输出。

For INPUT: abc and Enter I got OUTPUT: 97 98 99 10. ( the ASCII values) 对于INPUT:abc,然后输入Enter我得到OUTPUT:97 98 99 10.(ASCII值)

For INPUT Ctrl-D I got OUTPUT: -1 - at EOF. 对于INPUT Ctrl-D,我得到OUTPUT:-1-在EOF。 So I think -1 is the value for EOF. 所以我认为-1是EOF的值。

Try other inputs instead of Ctrl-D, like Ctrl-Z. 尝试使用其他输入代替Ctrl-D,例如Ctrl-Z。 I think it varies from compiler to compiler. 我认为它因编译器而异。

to keep it simple: EOF is an integer type with value -1. 为了简单起见:EOF是值为-1的整数类型。 Therefore, we must use an integer variable to test EOF. 因此,我们必须使用整数变量来测试EOF。

#include <stdio.h>

int main() {
    int c;
    while((c = getchar()) != EOF) { 
        putchar(c);
    }    
    printf("%d  at EOF\n", c);
}

modified the above code to give more clarity on EOF, Press Ctrl+d and putchar is used to print the char avoid using printf within while loop. 修改了上面的代码以更清楚地了解EOF,请按Ctrl + d,然后使用putchar来打印char,避免在while循环中使用printf。

int c;

while((c = getchar())!= 10)
{
    if( getchar() == EOF )
        break;

     printf(" %d\n", c);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM