9

I'm not good at C and I'm trying to do something simple. I want to open a binary file, read blocks of 1024 bytes of data and dump into a buffer, process the buffer, read another 1024 byes of data and keep doing this until EOF. I know how / what I want to do with the buffer, but it's the loop part and file I/O I keep getting stuck on.

PSEUDO code:

FILE *file;
unsigned char * buffer[1024];

fopen(myfile, "rb");

while (!EOF)
{
  fread(buffer, 1024);
  //do my processing with buffer;
  //read next 1024 bytes in file, etc.... until end
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mike
  • 121
  • 1
  • 1
  • 2

2 Answers2

27

fread() returns the number of bytes read. You can loop until that's 0.

FILE *file = NULL;
unsigned char buffer[1024];  // array of bytes, not pointers-to-bytes
size_t bytesRead = 0;

file = fopen(myfile, "rb");   

if (file != NULL)    
{
  // read up to sizeof(buffer) bytes
  while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0)
  {
    // process bytesRead worth of data in buffer
  }
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Hakim, if you see this... I rolled back your revision since (a) it conflicts with the intention of the code and (b) it actually introduced a bug. In particular, if the file is not an even multiple of 1024, your version would not read the last "chunk" of bytes, since it will *only* return 1 or 0, where "1" means "I read all 1024 bytes" and "0" means "I did not". – Paul Roub Mar 15 '22 at 19:43
  • Paul, sorry I thought you swapped by mistake the 2nd and 3rd arguments of `fread()`, at least as stated in the [docs](https://www.cplusplus.com/reference/cstdio/fread/) – Hakim Mar 16 '22 at 09:13
  • 1
    Nope. What am I reading? Bytes, which are size 1 -- that's the 'size' (second) parameter. How many bytes? Up to 1024, that's the 'count' (third) parameter. – Paul Roub Mar 16 '22 at 14:31
1
#include <stdio.h>
#include <unistd.h> // For system calls write, read e close
#include <fcntl.h>

#define BUFFER_SIZE 1024

int main(int argc, char* argv[]) {
    unsigned char buffer[BUFFER_SIZE] = {0};
    ssize_t byte = 0;
    
    int fd = open("example.txt", O_RDONLY);
    
    while ((byte = read(fd, buffer, sizeof(buffer))) != 0) {
        printf("%s", buffer);
        memset(buffer, 0, BUFFER_SIZE);
    }
    
    close(fd);
    
    return 0;
}

Edited code added

#include <stdio.h>
#include <unistd.h> // For system calls write, read e close
#include <fcntl.h>

#define BUFFER_SIZE 1024

int main(int argc, char* argv[]) {
    unsigned char buffer[BUFFER_SIZE] = {0};
    ssize_t byte = 0;
    
    // open file in read mode
    int fd = open("example.txt", O_RDONLY);
    
    // file opening failure
    if (fd == -1) {
        printf("Failed to open file\n");
        return -1;
    }
    
    // loop
    while (1) {
        // read buffer
        byte = read(fd, buffer, sizeof(buffer));
        // error
        if (byte == -1) {
            printf("Encountered an error\n");
            break;
        } else if (byte == 0) {
            // file end exit loop
            printf("File reading end\n");
            break;
        }
        
        // printf file data
        printf("%s", buffer);
        memset(buffer, 0, BUFFER_SIZE);
    
    }
    
    // Close file
    close(fd);
    
    return 0;
}
halloweeks
  • 19
  • 1
  • 5
  • `while ((byte = read(fd, buffer, sizeof(buffer))) != 0)`. This statement doesn't check if `read` returns an error. If `example.txt` file doesn't exsists, the loop keeps doing nothings except zeroing the buffer. – MaxPlankton Dec 06 '22 at 13:16
  • 1
    Just add condition check -1 error And check file if (fd== -1) { – halloweeks Dec 06 '22 at 13:35