LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fread() does not reach EOF in C (https://www.linuxquestions.org/questions/programming-9/fread-does-not-reach-eof-in-c-4175733904/)

lxs602 02-15-2024 05:35 PM

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.

dugan 02-15-2024 05:52 PM

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)) {

rclark 02-15-2024 06:48 PM

or for more readable

Code:

while (fread(buffer, BLOCK, 1, file) > 0)
  {
  // do something
  }


rnturn 02-15-2024 07:26 PM

Quote:

Originally Posted by lxs602 (Post 6483886)

[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...

sundialsvcs 02-16-2024 08:46 AM

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.

vmelkon 03-13-2024 11:45 AM

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);



All times are GMT -5. The time now is 01:47 AM.