The `feof` function is available as part of the C Standard Library and checks whether the end of a stream has been reached. Usually, when it is used, the code using it is wrong.
Questions tagged [feof]
157 questions
672
votes
5 answers
Why is “while( !feof(file) )” always wrong?
What is wrong with using feof() to control a read loop? For example:
#include
#include
int
main(int argc, char **argv)
{
char *path = "stdin";
FILE *fp = argc > 1 ? fopen(path=argv[1], "r") : stdin;
if( fp == NULL…

William Pursell
- 204,365
- 48
- 270
- 300
30
votes
3 answers
Why is fread reaching the EOF early?
I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file.
The loop runs…

James Schek
- 17,844
- 7
- 51
- 64
15
votes
2 answers
How feof() works in C
Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer?
Thanks for your help !

Alfred James
- 1,029
- 6
- 17
- 27
13
votes
2 answers
Is it possible to distinguish the error returned by fgets
Upon looking at the ISO C11 standard for fgets §7.21.7.2, 3, the return value is stated regarding the synopsis code:
#include
char *fgets(char* restrict s, int n, FILE* restrict stream);
The fgets function returns s if successful. If…

Miket25
- 1,895
- 3
- 15
- 29
13
votes
5 answers
Confusion with == EOF vs feof
I opened a file, the stream is found at the address of pointer ptr. I am attempting to see whether or not a file is blank. Using the following
if (fgetc(ptr) != EOF)
works as expected. When the file is blank, the statement is not executed. When the…
user2587754
9
votes
2 answers
how use EOF stdin in C
I need to input coordinates into an array until EOF is encountered, but something is wrong in my code. I used ctrl+Z, ctrl+D
int main()
{
int x[1000],y[1000];
int n=0,nr=0,a,b,i;
printf("Enter the coordinates:\n");
while(scanf ( "%d…

Trung Nguyen
- 91
- 1
- 1
- 2
8
votes
2 answers
Reading data from fsockopen using fgets/fread hangs
Here is the code that I am using:
if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
echo "Could not connect to host";
$server_response = fread($fp, 256);
echo $server_response;
fwrite($fp, "C01…

victor_golf
- 195
- 2
- 2
- 10
7
votes
5 answers
How does feof() actually know when the end of file is reached?
I'm a beginner in C++ and trying to better understand feof(). I've read that feof() flag is set to true only after trying to read past the end of a file so many times beginners will read once more than they were expecting if they do something like…

Austin
- 6,921
- 12
- 73
- 138
6
votes
3 answers
PHP's feof behavior versus C's
I come from a C and C++ background but also play with some web stuff. All us C folks (hopefully) know that calling feof on a FILE* before doing a read is an error. This is something that stings newbies to C and C++ very often. Is this also the case…

Evan Teran
- 87,561
- 32
- 179
- 238
5
votes
2 answers
feof generates infinite loop
So I do one simple thing, firstly I exec command via ssh2_exec (after success authenticate) and then read answer in variable. My code below (without authenticate)
try {
$stdout_stream = ssh2_exec($this->connection, $_cmd);
…

Alexandr Suleymanov
- 363
- 3
- 8
4
votes
3 answers
Reading names in a file in C
I have a file like this :
name1 nickname1
name2 nickname2
name3 nickname3
And I want my programm read that file and show the name/nicknames couples.
Here is what I did :
users_file = fopen("users", "r");
while(!feof(users_file))
{
…

kyori
- 487
- 1
- 7
- 21
4
votes
2 answers
feof wrong loop in c
I use below code to read a char from file and replace it with another,
but I have an error.loop in going to end of file.
What is wrong?
I tested this code on linux (netbeans IDE) and it was correct and worked beautiful but when I tried to use VS…
user787084
3
votes
0 answers
while(!feof) don't put data into variables
I'm just start learning php and have a question about feof().
I wanna read 2 strings from txt file and put it into two variables. But something going wrong, because var_dump show me FALSE.
$fbhand = fopen("c://study/file.txt","r");
while…

texas_panda
- 41
- 4
3
votes
2 answers
3
votes
2 answers
Warning: feof() expects parameter 1 to be resource, boolean given in /volume1/web/comment.php on line 62
I'm only starting to learn code with the help of youtube and other sites, and I've run tino a problem. Here's my code:

Katsu
- 41
- 2
- 5