I know that in C++ the EOF character is automatically appended at the end of the file by statements like filestream.close. I wanted to know, is this done in C also by default?
-
3There's no "EOF character" appended to files. Create a file with C++'s `ofstream`, then hexdump it and you'll see nothing apart from what you're written to it yourself. `EOF` is simply a constant returned by some C I/O functions to indicate that there's nothing in the stream left for you to read. – user4520 May 07 '16 at 18:15
-
1No `fclose` doesn't add any `CTRL+Z` (or EOF char of DOS times), but when opening an ANSI text stream or using `fopen` with `'t'`, text qualifier, MS still checks for the presence of a `CTRL+Z` char, truncate the file and remove it to avoid wrong `fseek` and `ftell` behavior. – Frankie_C May 07 '16 at 18:31
4 Answers
EOF
is not a character. It is a non-character int
value returned by some operations to indicate that the end of file was reached. So the question becomes "Does the fclose()
function in C closes the file" which is of course the case.

- 20,415
- 4
- 46
- 67
There is no EOF
character. EOF
by definition "is unequal to any valid character code". Often it is -1
. It is not written into the file at any point.
There is a historical EOF character value (CTRL+Z)
in DOS, but it is obsolete these days.
To answer your question fclose
is very similar to filestream.close
and closes the file.
Also refer : reference answer
-
-
-
If a question is already answered, you should vote for closing it as a "duplicate of...". – Elazar May 07 '16 at 18:24
-
@Elazar kindly check the reference given & also the question there. A question is a duplicate if the question is the same not when the answers are same. – Ani Menon May 07 '16 at 18:26
-
`^Z` is supposed to be `SUB`: https://en.wikipedia.org/wiki/C0_and_C1_control_codes#C0_.28ASCII_and_derivatives.29 – o11c May 07 '16 at 18:37
-
@o11c `^Z`(Control+Z) is used to signal an end-of-file, and thus known as the EOF character in `DOS`. [Read this](https://en.wikipedia.org/wiki/Control-Z) – Ani Menon May 07 '16 at 18:44
If there were an explicit end-of-file character which needed writing to the file, then yes, fclose
would arrange to write it. You would not have to write it yourself.
For example, once upon a time, text files under MS-DOS had explicit control-Z end-of-file characters at their ends. In C under that OS, I would have expected the control-Z character to get written when I called fclose
. (But with that said, it's been so long since used MS-DOS, I can't say I remember for sure watching this happen.)
Today, as other answers have said, none of the operating systems we care about require explicit end-of-file characters in on-disk files, so the question is moot.

- 45,437
- 7
- 70
- 103
It does, but not as character.(See in comments under your Question)
But, you can even check it with (feof(file))
. It's like a STATE.

- 64
- 4
-
While you're correct about `feof`, as pointed out in the comments there's no such thing as an "EOF character". Also, you might also want to mention how to use the function correctly because many people have problems with that: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – user4520 May 07 '16 at 18:17