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
3
votes
3 answers
PHP feof() returning true before the end of file
I have been working on a strange PHP problem the last few days where the feof() function is returning true before the end of a file. Below is a skeleton of my code:
$this->fh = fopen("bigfile.txt", "r");
while(!feof($this->fh))
{
…

user2395126
- 526
- 1
- 7
- 20
3
votes
5 answers
feof() in C file handling
I am reading a binary file byte-by-byte,i need determine that whether or not eof has reached.
feof() doesn't works as "eof is set only when a read request for non-existent byte is made". So, I can have my custom check_eof like:
if (…

sud03r
- 19,109
- 16
- 77
- 96
3
votes
3 answers
Why is this C code buggy?
On another question, Jerry Coffin pointed out the following:
It's (probably) not really related to your question, but while (!feof(fileptr)){ is pretty much a guaranteed bug.
I figured I would start a separate question since that comment is…

Jason Baker
- 192,085
- 135
- 376
- 510
3
votes
4 answers
Using fscanf() using feof()
Here's my code.
#include
void main(){
FILE *fp;
int a,b;
fp=fopen("hello.txt","r");
while(!feof(fp)){
fscanf(fp,"%d %d",&a,&b);
printf("%d %d\n",a,b);
}
}
My hello.txt is
1 2
3 4
My Output is
1 2
3 …

Kraken
- 23,393
- 37
- 102
- 162
3
votes
2 answers
fopen on file through FTP URL
I am trying to download a file using PHP from an FTP URL, which looks like ftp://username:password@domain/file.zip .
The URL is good, because I can download the file pasting it into any browser.
Fopen supports this kind of file streaming, just saw…

Ervin
- 2,374
- 4
- 30
- 44
2
votes
3 answers
What is the difference between feof() function and EOF of a file in C?
I couldn't understand what is the difference between the feof() function and EOF ? Both represent the end of file, then how are they different and how would we know where to use what?
Where to use the feof() function and where to use EOF?

sunflower
- 21
- 3
2
votes
1 answer
Why does my program print something before it ends when I press ctrl + D?
So I wrote a simple program that converts a decimal to binary, that only accepts positive whole numbers. So numbers like -2 and 1.1 would output "Sorry, that's not a positive whole number." It infinitely asks the user to input a number until the…

osito_solito
- 65
- 5
2
votes
2 answers
determine size of file for buffer allocation in c
This snippet of code I wrote is supposed to traverse through
a file and increment the size-counter each-time it goes +1
character:
while (fgetc(input) != EOF)
size++;
if (feof(input)) {
buff = (char *)malloc(sizeof(char) * size + 1);…

real_G
- 49
- 8
2
votes
3 answers
Read an entire pipe - c
I've got some difficulties with this code.
I need to get all the information from the pipe at its end.
But, I get a segfault error.
#include
#include
#include
int main(void){
int tube[2];
if(pipe(tube) ==…

ztarky
- 53
- 1
- 5
2
votes
1 answer
getline returns -1, EOF not set, errno not set, when given very large input
I did not think these three conditions can happen at the same time. I have:
char* line = NULL;
size_t capacity = 0;
ssize_t n = getline(&line, &capacity, stdin);
if (n == -1) {
int err = errno; // preserve it
if (feof(stdin) == 0) { // means…

djechlin
- 59,258
- 35
- 162
- 290
2
votes
4 answers
feof() returning true when EOF is not reached
I'm trying to read from a file at a specific offset (simplified version):
typedef unsigned char u8;
FILE *data_fp = fopen("C:\\some_file.dat", "r");
fseek(data_fp, 0x004d0a68, SEEK_SET); // move filepointer to offset
u8 *data = new…

Daniel Sloof
- 12,568
- 14
- 72
- 106
2
votes
3 answers
How to use feof(FILE *f)?
I'm having a hard time with a do-while loop, that is supposed to stop
when we reach the end of the file. Here's the loop code:
do {
if (pcompanyRow[0] != '#' && pass == 1) {
strtok(pcompanyRow, ":");
pcompanyName =…

k-man
- 1,121
- 5
- 17
- 26
2
votes
1 answer
can't print txt file correctly
Hi i have this text file, where the first column in a character the 2nd and the third one is an integer.. but I'm not able to read and print the values correctley.
So this the file am trying to read:
c 6
o 4 3
o 2 4
o 3 2
o 1 1
o 3 3
And here is…

MongJi
- 117
- 2
- 6
2
votes
4 answers
Reading unknown number of integers from a file
This is a function that reads an unknown number of integers from a file and calculates the average. For some reason the last element in the file is being read twice. Can someone please explain why? I know how to fix it but would like to know what…

Ace
- 462
- 4
- 10
- 17
2
votes
2 answers
feof() on Linux return true at one line later than the ending line
I'm a beginner of C. When I use this while loop to print the contains of a file. The last line will print twice on Linux. It should not get into while loop when reach the end of file. It has no problem on windows.
#include
#include…

Dan
- 339
- 1
- 3
- 6