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

时间:2021-07-03 15:04:03

How do you get to see the last print? In other words what to put in for EOF? I checked the definitions and it says EOF is -1.

你怎样才能看到最后的印刷字体?换句话说,什么是EOF?我检查了定义,它说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);
}

9 个解决方案

#1


41  

On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.

在Linux系统和OS X上,输入导致EOF的字符是Ctrl-D。对于Windows,ctrl - z。

Depending on the operating system, this character will only work if it's the first character on a line, i.e. the first character after an 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字符。

And yes, if that character is recognized as an EOF, then your program will never see the actual character. Instead, a C program will get a -1 from getchar().

是的,如果这个字符被识别为EOF,那么你的程序将永远看不到实际的字符。相反,一个C程序将从getchar()得到一个-1。

#2


26  

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". This will always become one for the character you entered and then the "\n" that follows by hitting return. Except for the last comparison where c really is EOF it will give you a 0.

您将得到两个1作为输出,因为您正在进行比较“c!=EOF”。这将永远是一个你输入的字符,然后是“\n”,然后点击返回。除了最后一个比较,c真的是EOF会给你一个0。

EDIT about EOF: EOF is typically -1, but this is not guaranteed by the standard. The standard only defines about EOF in section 7.19.1:

编辑EOF: EOF通常是-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时不应该对特定值进行测试,而是使用宏。

#3


7  

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. 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.

在UNIX/Linux上的控制台流中,您输入的是D字符,以便在逻辑上结束输入流。但在其他情况下(比如从文件中读取数据),它只是另一个数据字符。无论哪种方式,^ D字符结束(即输入)从来没有使它应用程序代码。

As @Bastien says, EOF is also returned if getchar() fails. Strictly speaking, you should call ferror or feof to see whether the EOF represents an error or an end of stream. But in most cases your application will do the same thing in either case.

正如@Bastien所说,如果getchar()失败,EOF也会返回。严格地说,您应该调用ferror或feof来查看EOF是否表示错误或流的结束。但在大多数情况下,你的应用程序在这两种情况下都会做同样的事情。

#4


4  

EOF means end of file. It's a sign that the end of a file is reached, and that there will be no data anymore.

EOF的意思是文件的结束。这标志着文件的结束,不再有数据了。

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)通过的。

#5


4  

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. Generally it is an int with all bits set.

还有getchar()将返回键作为有效的输入,因此您也需要缓冲它。EOF是表示输入结束的标记。通常它是一个所有位集的整数。


#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>

#6


3  

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. By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. 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). AmigaDOS is similar but uses Control-\ instead of Control-D.

从终端输入的nput永远不会真正“结束”(除非设备断开连接),但是将多个“文件”输入到终端是有用的,因此保留一个键序列来表示输入端。在UNIX中,由终端驱动程序执行键到EOF的转换,因此程序不需要将终端与其他输入文件区分开来。默认情况下,驱动程序在一行开始时将一个Control-D字符转换为文件结束指示符。要在输入流中插入一个实际的Control-D (ASCII - 04)字符,用户在其前面加上一个“quote”命令字符(通常是Control-V)。AmigaDOS是类似的,但使用控制-\而不是控制- 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. 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. This was done for two reasons:

在微软的DOS和Windows(以及CP/M和许多DEC操作系统)中,从终端读取数据永远不会产生EOF。相反,程序识别源是一个终端(或其他“字符设备”),并将给定的保留字符或序列解释为文件结束指示符;通常这是一个ASCII控件- z,代码26。一些MS-DOS程序,包括微软MS-DOS shell (COMMAND.COM)和操作系统实用程序(如EDLIN)的部分,将控制- z放在文本文件中,以标记有意义的数据的结束,或者在编写文本文件时将Control-Z添加到最后。这样做有两个原因:

  1. Backward compatibility with 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. The MS-DOS filesystem has always recorded the exact byte-length of files, so this was never necessary on MS-DOS.

    对CP / M的向后兼容性。CP/M文件系统只记录了128字节“记录”中文件的长度,因此,按照惯例,如果在记录的中间结束,就会使用Control-Z字符来标记有意义的数据的结束。MS-DOS文件系统总是记录文件的精确字节长度,因此在MS-DOS中这是不需要的。

  2. It allows programs to use the same code to read input from both a terminal and a text file.

    它允许程序使用相同的代码来读取终端和文本文件的输入。

#7


1  

#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. And I checked the output.

我认为这是检验EOF价值的正确方法。我检查了输出。

For INPUT: abc and Enter I got OUTPUT: 97 98 99 10. ( the ASCII values)

输入:abc,输入输出:97 989910。(ASCII值)

For INPUT Ctrl-D I got OUTPUT: -1 - at EOF. So I think -1 is the value for EOF.

对于输入Ctrl-D,我得到输出:-1 -在EOF。我认为-1是EOF的值。

Try other inputs instead of Ctrl-D, like Ctrl-Z. I think it varies from compiler to compiler.

尝试其他输入,而不是Ctrl-D,比如Ctrl-Z。我认为它不同于编译器和编译器。

#8


-1  

#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打印字符避免在while循环中使用printf。

#9


-1  

int c;

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

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

#1


41  

On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.

在Linux系统和OS X上,输入导致EOF的字符是Ctrl-D。对于Windows,ctrl - z。

Depending on the operating system, this character will only work if it's the first character on a line, i.e. the first character after an 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字符。

And yes, if that character is recognized as an EOF, then your program will never see the actual character. Instead, a C program will get a -1 from getchar().

是的,如果这个字符被识别为EOF,那么你的程序将永远看不到实际的字符。相反,一个C程序将从getchar()得到一个-1。

#2


26  

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". This will always become one for the character you entered and then the "\n" that follows by hitting return. Except for the last comparison where c really is EOF it will give you a 0.

您将得到两个1作为输出,因为您正在进行比较“c!=EOF”。这将永远是一个你输入的字符,然后是“\n”,然后点击返回。除了最后一个比较,c真的是EOF会给你一个0。

EDIT about EOF: EOF is typically -1, but this is not guaranteed by the standard. The standard only defines about EOF in section 7.19.1:

编辑EOF: EOF通常是-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时不应该对特定值进行测试,而是使用宏。

#3


7  

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. 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.

在UNIX/Linux上的控制台流中,您输入的是D字符,以便在逻辑上结束输入流。但在其他情况下(比如从文件中读取数据),它只是另一个数据字符。无论哪种方式,^ D字符结束(即输入)从来没有使它应用程序代码。

As @Bastien says, EOF is also returned if getchar() fails. Strictly speaking, you should call ferror or feof to see whether the EOF represents an error or an end of stream. But in most cases your application will do the same thing in either case.

正如@Bastien所说,如果getchar()失败,EOF也会返回。严格地说,您应该调用ferror或feof来查看EOF是否表示错误或流的结束。但在大多数情况下,你的应用程序在这两种情况下都会做同样的事情。

#4


4  

EOF means end of file. It's a sign that the end of a file is reached, and that there will be no data anymore.

EOF的意思是文件的结束。这标志着文件的结束,不再有数据了。

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)通过的。

#5


4  

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. Generally it is an int with all bits set.

还有getchar()将返回键作为有效的输入,因此您也需要缓冲它。EOF是表示输入结束的标记。通常它是一个所有位集的整数。


#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>

#6


3  

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. By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. 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). AmigaDOS is similar but uses Control-\ instead of Control-D.

从终端输入的nput永远不会真正“结束”(除非设备断开连接),但是将多个“文件”输入到终端是有用的,因此保留一个键序列来表示输入端。在UNIX中,由终端驱动程序执行键到EOF的转换,因此程序不需要将终端与其他输入文件区分开来。默认情况下,驱动程序在一行开始时将一个Control-D字符转换为文件结束指示符。要在输入流中插入一个实际的Control-D (ASCII - 04)字符,用户在其前面加上一个“quote”命令字符(通常是Control-V)。AmigaDOS是类似的,但使用控制-\而不是控制- 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. 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. This was done for two reasons:

在微软的DOS和Windows(以及CP/M和许多DEC操作系统)中,从终端读取数据永远不会产生EOF。相反,程序识别源是一个终端(或其他“字符设备”),并将给定的保留字符或序列解释为文件结束指示符;通常这是一个ASCII控件- z,代码26。一些MS-DOS程序,包括微软MS-DOS shell (COMMAND.COM)和操作系统实用程序(如EDLIN)的部分,将控制- z放在文本文件中,以标记有意义的数据的结束,或者在编写文本文件时将Control-Z添加到最后。这样做有两个原因:

  1. Backward compatibility with 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. The MS-DOS filesystem has always recorded the exact byte-length of files, so this was never necessary on MS-DOS.

    对CP / M的向后兼容性。CP/M文件系统只记录了128字节“记录”中文件的长度,因此,按照惯例,如果在记录的中间结束,就会使用Control-Z字符来标记有意义的数据的结束。MS-DOS文件系统总是记录文件的精确字节长度,因此在MS-DOS中这是不需要的。

  2. It allows programs to use the same code to read input from both a terminal and a text file.

    它允许程序使用相同的代码来读取终端和文本文件的输入。

#7


1  

#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. And I checked the output.

我认为这是检验EOF价值的正确方法。我检查了输出。

For INPUT: abc and Enter I got OUTPUT: 97 98 99 10. ( the ASCII values)

输入:abc,输入输出:97 989910。(ASCII值)

For INPUT Ctrl-D I got OUTPUT: -1 - at EOF. So I think -1 is the value for EOF.

对于输入Ctrl-D,我得到输出:-1 -在EOF。我认为-1是EOF的值。

Try other inputs instead of Ctrl-D, like Ctrl-Z. I think it varies from compiler to compiler.

尝试其他输入,而不是Ctrl-D,比如Ctrl-Z。我认为它不同于编译器和编译器。

#8


-1  

#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打印字符避免在while循环中使用printf。

#9


-1  

int c;

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

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