7

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
  • 35,848
  • 27
  • 79
  • 88
Mike
  • 101
  • 1
  • 1
  • 2

1 Answers1

25

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
  • 35,848
  • 27
  • 79
  • 88
  • 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