Analysis of C + + scanf function and EOF

Source: Internet
Author: User

"Summary"

This code is often encountered, while (scanf ("%d", &num)!=eof) {...}, the return value of EOF and the scanf function has not been well researched, this article will be a brief analysis of the problem.


Body


"scanf function return value"
the 1.scanf () function has a return value and is of type int.
The value returned by the 2.SCANF () function is: Enter the number of variables correctly in the specified format, or the number of variables that can receive the value correctly.
3. When the first variable of the scanf function is malformed, the return value is 0, that is, when the nth variable of the scanf function is malformed, the return value is n-1;

4. If you enter ctrl+d in the terminal at scanf, or if you enter the exception directly, the return value of scanf will be-1.


"What is EOF?"
EOF is an abbreviation for the end of file that represents the end of a text stream (stream). The "text stream" here can be either a file or a standard input (stdin). Naturally, I thought, at the end of each file, there is a special character called EOF, which reads to this character and the operating system thinks the file is over. However, later I found that EOF is not a special character, but a constant defined in the header file stdio.h, which is generally equal to-1.

#define EOF (-1)


in a Linux system, EOF is not a character at all, but a signal value (i.e.-1) returned when the system reads to the end of the file. As for how the system knows the end of the file, it says by comparing the length of the file.
Therefore, the processing file can be written as follows:
int C;
While ((c = fgetc (FP))! = EOF)
      {
Do something
}
However, there are also problems with this writing. FGETC () After reading the last character of the file, the C feof () function still returns 0, indicating that the end of the file is not reached; only if FGETC () reads a character backwards (that is, over the last character), feof () returns a non-0 value indicating the end of the file.

So, in this way, if a file contains n characters, then the internal operation of the while loop runs n+1 times. So the safest way to do this is as follows:

int c = fgetc (FP);
While (c! = EOF)
      {
Do something;

C = fgetc (FP);
}
if (feof (FP)) {
printf ("\ n End of file reached.");
} else {
printf ("\ n Something went wrong.");
}
In addition to representing the end of a file, EOF can also represent the end of a standard input.
int C;
While ((c = GetChar ())! = EOF)
      {
Putchar (c);
}
However, the standard input is not the same as the file, the length of the input cannot be known beforehand, and a character must be entered manually to indicate that EOF is reached.
Linux, at the beginning of a new line, press ctrl-d, which represents EOF (if you press ctrl-d in the middle of a row, the output "standard input" buffer, so you must press two times ctrl-d); In Windows, Ctrl-z represents EOF. (Incidentally, by pressing Ctrl-z in Linux, the process is interrupted, suspended in the background, the FG command can be re-cut back to the foreground, and the process is terminated by pressing CTRL-C.) )
So, what if you really want to enter ctrl-d? At this point you must press CTRL-V, then you can enter the ctrl-d, the system will not think this is the EOF signal. Ctrl-v said that by "literal meaning" to interpret the next input, if you want to press "literal meaning" input ctrl-v, input two consecutive times on the line.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of C + + scanf function and EOF

Related Article

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.