Differences between file Terminator EOF feof

Source: Internet
Author: User

 

Address:
Http://bbs.chinaunix.net/viewthread.php? Tid = 981231 & extra = & page = 2

I don't know the differences between the two. I don't know how worried it is. I just can't understand it. I can't figure it out. It's a long time, but I still don't understand it, I don't know when to use the EOF feof. It's always confusing .... unclear,

Why can't these two differences be understood? I am confused about the online resources. I just want to find them ....

Looking for... looking ....
Find ..... find .... find .... no one will tell you the difference. One is to teach yourself, and the other is the best teacher in the world.
Focus:

Quote: original post SiyikePublished on
The return value of getchar () should be defined as Int. Instead of char.

I learned from the experts.

The moderator is right. getchar is defined as int getchar (void) and INT is returned.

Published by whyglinux in 2008-6-6 22: 46
> About the file Terminator EOF

EOF is the abbreviation of end of file.

In C language, it is a macro defined in the standard library.

People often mistakenly believe that EOF is a character read from a file. In fact, EOF is not a character and is defined as an int type.
Standard. Positive solution.

> About the file Terminator EOF

EOF is the abbreviation of end of file.

In C language, it is a macro defined in the standard library.

People often mistakenly think that EOF is a character read from a file (remember ). In fact, EOF is not a character. It is defined as a negative number (for example,-1) of the int type ). EOF is not the actual content in the file. EOF does not only indicate that the read file is in the final state (this state can be detected using feof ), it also indicates read and write errors in I/O operations (ferror () can be used to detect) and the error states of some other associated operations.

Getchar returns EOF if the end of the file is read

For classic masters, you must read and understand them carefully. I used to check the C programming language (secondedition) of K & R)
The character input/output in section 1.5 is confused by getchar () and EOF. The reason may be that the working principle and EOF usage of getchar () are not clear. Therefore, it is necessary to sum up. Otherwise, many trivial knowledge points will be forgotten after a long time. Only writing down is the best method.

Actually, the most typical getchar () program is just a few lines of code. My environment is debiangnu/Linux, and the same is true in other systems.
I. Two points of getchar summary:
1. getchar is accessed in the unit of behavior.
When getchar is used for input, if the first character entered is a valid character (that is, the input is the file Terminator EOF, Windows is the combination key Ctrl + Z, in Unix/Linux, press Ctrl + D. Only when the last input character is the linefeed '\ n' (or the file Terminator EOF, which will be discussed later, getchar stops execution and the entire program is executed. For example, the following program section:

While (C = getchar ())! = EOF ){
Putchar (C );
}

Run the program, enter ABC, and press Enter. Then the program will execute puchar (c) And then output ABC. Do not forget this. The system outputs a carriage return. Then you can continue the input. When you encounter a linefeed again, the program will output the input characters of that line on the terminal.

For getchar, many beginners may ask, isn't getchar read in character units? Now that I have entered the first character "A", it must satisfy the while loop (C = getchar ())! = EOF condition, you should execute putchar (c) to output a character a on the terminal. Yes, I keep thinking this way when using getchar, but the program does not execute like a sample. Instead, it must read a linefeed or file Terminator EOF to output it once.

One explanation of this problem is that when the master wrote C, there was no so-called terminal input concept at the time, and all input was actually read according to the file, files are generally behavior units. Therefore, only when a line break is encountered, the program considers the input to end and then takes other parts of the execution program. At the same time, the input is accessed by file, so to end the input of a file, you need to use EOF (enf of file ). this is why EOF is used when the getchar end input exits.

2. the return value of getchar () is generally a character, but it may also be a negative value, that is, EOF is returned.

It should be emphasized that the getchar function usually returns the characters entered by the terminal. the ASCII values in these character systems are non-negative. Therefore, we often write the following two lines of code:

Char C;
C = getchar ();

In this way, problems may occur. In addition to the characters entered by the terminal, the getchar function returns EOF when Ctrl + d (in Linux) is the file Terminator EOF, this EOF is generally defined as-1 in the function library. Therefore, in this case, the getchar function returns a negative value and it is incorrect to assign a negative value to a char variable. To allow the Defined variables to include all possible values returned by the getchar function, the correct definition method is as follows (this problem is not mentioned in K & r c ):

Int C;
C = getchar ();

Ii. Two points of EOF (EOF in general terminals)
1. When EOF is used as the file Terminator:

Although EOF is a file Terminator, it does not enable the file termination function by entering Ctrl + d (CTRL + Z in Windows) under any circumstances, is used as the file Terminator.
(1) When the getcahr function is executed, press Ctrl + D when the first character is entered, and the getchar () can be jumped out to execute other parts of the program;
(2) When the character entered earlier is a line break, press Ctrl + D;
(3) When there is a character input before and it is not a line break, enter Ctrl + d twice in a row. Then, the CTRL + D entered for the second time serves as the end character of the file, the role of Ctrl + D for the first time will be described below.
In fact, all three cases can be summarized as that only when getchar () prompts a new input, directly entering Ctrl + D is equivalent to the file Terminator.

2. When EOF is used as the row Terminator, entering Ctrl + D does not end getchar (), but only triggers getchar () to prompt the next round of input.

In this case, when a new row of getchar () is input, when a certain number of characters (cannot contain line breaks) are input, press Ctrl + D, at this time, CTRL + D is not the end of the file, but is equivalent to the line break function, that is, to end the current input. The preceding code segment is used as an example. If ABC is input during execution and CTRL + D is input, the output result of the program is:
Abcabc

Note: the first group of ABC is input from the terminal, and then Ctrl + D is input, the second group of ABC is output, and the cursor stops behind the second group of characters C, and then a new input can be made. If Ctrl + D is entered again, the end of the file is ended with getchar ().
If you enter ABC, press enter, and enter a line break, the terminal will display:
ABC // The first line, with carriage return
ABC // The second line
// The third line

The first act is terminal input, the second act is terminal output, and the cursor stops at the third line, waiting for a new terminal input.
We can also see the different output results when Ctrl + D and linefeed are used as the line terminator respectively.
The role of EOF can also be summarized as: when there is a character input in the terminal, the EOF generated by Ctrl + D is equivalent to ending the input of this line, which will lead to a new round of input of getchar; if the terminal does not have a character input, or you can say that when getchar () reads a new input, press Ctrl + D. The generated EOF is equivalent to the file Terminator, and the program ends the execution of getchar.
[Supplement] The summary of EOF in the second part of this article is applicable to the mode where the terminal driver is in one row. That is, although getchar () and putchar () are based on one character each time. However, the terminal driver is in the one-row mode, and its input ends only when "\ n" or EOF. Therefore, the output obtained on the terminal is also row-based.
If you want to enable the terminal to end the input after reading a character, the following Program is an implementation method (refer to "C expert programming", slightly changed)

/* Edit by godbach
Cu blog: http://blog.chinaunix.net/u/33048/
*/
# Include <stdio. h>
# Include <stdlib. h>

Int
Main (void)
{
Int C;
/* The Terminal Driver is in normal one-row mode */
System ("stty raw ");

/* The current terminal driver is in the one-Character Mode at a time */
C = getchar ();
Putchar ();

/* Return to the One-row mode again at the terminal driver */
System ("stty cooked ");

Return 0;
}

Compile and run the program. When a character is entered, the program will start with one character and end.
It can be seen that the conditions for the end of the getchar () input are different due to different terminal-driven modes. In normal mode, you need to press ENTER or EOF, while in one character at a time, it will end after one character is entered.

I hope this article can help anyone who is new to C and communicate with others. I am very grateful for the correction and suggestions for any mistakes I have made. At the same time, this article references chinaunix.net's post on getchar and a blog post. The link addresses are:
Http://blog.chinaunix.net/u/9861/showart_64652.html
Http://bbs.chinaunix.net/viewthread.php? Tid = 679688 & extra = & page = 1
Thank you for your comments and comments.
In another post, I had some discussions with some friends about GETC, because I thought the landlord could not understand it.
So I sum up my personal opinions and write them here. I am not very good at illustration, but I have tried my best.
Anyone who transfers this post must write your name in a conspicuous position.

The agreed compiler is gcc2/x86:
So char, unsigned char is 8 bits, Int Is 32 bits

See http://bbs.chinaunix.net/forum/23/20031223/229236.html

(1) byte reading

Normally, GETC reads the file stream as an unsigned char, expands to an integer, and returns
Back. In other words, GETC takes a byte from the file stream and adds 24 zeros to become an integer smaller than 256,
Then return.

Int C;
While (C = fgetc (RFP ))! =-1) //-1 is EOF
Fputc (C, WFP );

Although C in fputc is an integer, the integer is 24 bits in height before fputc writes it to the file stream.
Removed, so fgetc and putc can be used together to implement File Replication. So far, C has been defined
Char is still feasible, but we will see below that defining C as int is to determine whether the segment file is complete correctly.

(2) determine the end of the file.

Most people think that there is an EOF in the file, which is used to indicate the end Of the file. However, this is actually incorrect.
There is no file terminator in the data contained in the file. For GETC, if it cannot be read from the file,
Returns an integer-1, which is the so-called EOF. The returned EOF is nothing more than two situations.
After reading; second, an error occurred while reading the file.

Note: Normally, the returned integers are less than 256, that is, 0x0 ~ 0xff. but cannot read the returned results
It is 0xffffffff. However, if you use fputc to write 0 xffffffff to the file, the 24-bit high will be blocked, and the write Will
It is 0xff. // lixforalpha. Please note this

(3) will 0xff confuse us?

No, provided that the C that receives the returned value is defined as int according to the prototype.

If the next read character is 0xff

Int C;
C = fgetc (RFP); // C = 0x000000ff;
If (C! =-1) // Of course,-1 is 0 xffffffff
Fputc (WFP); // Oh, oxff copied successfully.

The character 0xff, which is not EOF.

(4) Define c as char

Assume that the next read character is 0xff.

Char C;
C = fgetc (RFP); // The value of fgetc (RFP) is 0x000000ff, Which is secretly reduced to bytes. c = 0xff
If (C! =-1) // compare the character with the integer? C is expanded to 0 xffffffff by signed, oh,
If the condition is true, File Replication exits early.

While (C = fgetc (RFP ))! = EOF! Unexpected suspension.

(5) Define c as unsigned char;

When you read the end of the file and return EOF, that is,-1,

Unsigned char C;
C = fgetc (RFP); // The value of fgetc (RFP) is EOF, that is,-1, that is, 0 xffffffff, in bytes, c = 0xff
If (C! =-1) // C is extended to 0x000000ff and never returns 0 xffffffff

Therefore, although 0xff can be correctly copied this time, the end of the file cannot be determined. In fact, when C is uchar,
C! =-1 is always true. A high-quality compiler, for example, GCC will point this point during compilation.

(6) Why feof?
File * FP;
FP points to a very complex data structure. feof uses the mark in this structure to determine whether the file ends.
If the file is read using fgetc, when the last character is read, The EOF mark in FP will not be opened.
If you use feof to determine whether the file is closed, the conclusion is displayed.

When fgetc returns-1, we still cannot be sure that the file has ended, because it may be a read error! Then we
Feof and ferror are required.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.