LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2024, 05:35 PM   #1
lxs602
Member
 
Registered: Oct 2018
Distribution: Ubuntu 22.04
Posts: 36

Rep: Reputation: 1
fread() does not reach EOF in C


Hi, this is a beginner question.

Code:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// Define block size
#define BLOCK 512

void function(char *argv);

int main(int argc, char *argv[])
{
    // Accept one input file
    if (argc != 2)
    {
        printf("Usage: ./Test inputfile\n");
        return 1;
    }

    // printf("argv: %s", argv[1]);

    // Open file and check success, as otherwise defaults to NULL.
    FILE *file = fopen(argv[1], "r");

    if (file == NULL)
    {
        printf("File not valid.\n");
        return 1;
    }

    // Allocate memory buffer with the defined block size
    uint8_t *buffer = malloc(BLOCK * sizeof(uint8_t));
    int counter = 0;

    
    while (fread(buffer, BLOCK, 1, file) != EOF)
    {
        // Printf as a test until EOF 
        printf("test\n");

        // Do something...
    }
    
    // Free memory and close file
    free(buffer);
    fclose(file);


    return 0;
}
This programme should read an input file in 512 byte blocks until reaching EOF, which it does not seem to reach. The line for `printf("test\n") I added prints lines of test indefinitely. Can anyone tell me what I am doing wrong?

Thanks.
 
Old 02-15-2024, 05:52 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Your code assumes that fread returns the value of the byte read, which could be EOF. It doesn't. It returns the number of "blocks" read.

So it should be:

Code:
while (fread(buffer, BLOCK, 1, file)) {
 
1 members found this post helpful.
Old 02-15-2024, 06:48 PM   #3
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
or for more readable

Code:
while (fread(buffer, BLOCK, 1, file) > 0) 
   {
   // do something
   }
 
1 members found this post helpful.
Old 02-15-2024, 07:26 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by lxs602 View Post

[snip]

This programme should read an input file in 512 byte blocks until reaching EOF, which it does not seem to reach. The line for `printf("test\n") I added prints lines of test indefinitely. Can anyone tell me what I am doing wrong?

Thanks.
From the fread(3) manpage:

Code:
RETURN VALUE
       On  success,  fread()  and  fwrite()  return the number of items read or written.  This number
       equals the number of bytes transferred only when size is 1.  If an error occurs, or the end of
       the file is reached, the return value is a short item count (or zero).

       fread() does not distinguish between end-of-file and error, and callers must use feof(3) and
       ferror(3) to determine which occurred.
One thing to try: Looping on calls to feof and freading only if it returns "nope, not at EOF yet.

Hope this points to a solution...
 
Old 02-16-2024, 08:46 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Reply #2 uses the fact that "zero is 'false-y'" and thus ends the while loop. This is very commonly done. But, the equivalent reply #3 is more explicit.
 
2 members found this post helpful.
Old 03-13-2024, 11:45 AM   #6
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Rep: Reputation: 84
Another solution is to find out the size of the file and read the entire file in 1 shot.

Code:
int fileSize;
FILE *file = fopen(argv[1], "r");
fseek(file, 0, SEEK_END);		//Go to end of file
fileSize=(int)ftell(file);	//This returns a long, which is 32 bit (64 bit warning!)
fclose(file);

file = fopen(argv[1], "r");
if(fread(buffer, 1, fileSize, file)< fileSize)
{
Error Detected;
}

fclose(file);
 
1 members found this post helpful.
  


Reply

Tags
beginner



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fread and fwrite seem to only work when using text files and not with jpgs retroCheck Programming 10 02-05-2015 05:23 AM
fread magic : input/output error-----what does this mean and can it be corrected fborga Linux - Newbie 2 10-26-2012 01:16 PM
How to reach information of a CDROM after apparent EOF? eantoranz Linux - Hardware 4 08-16-2010 06:34 PM
fread can not pass the exact "count" ... intermilan Linux - Newbie 5 09-27-2009 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration