fread function

Prototype size_t fread (void * Buffer, size_t Size, size_t Count, FILE * Stream);
PurposeTo read data from a file stream.
Header File#include <stdio.h>
Parameters
BufferPointer to an area of variables where the data is to be placed
SizeSize of each variable item
CountNumber of variable items
StreamPointer to an open FILE
Return Value Count of items actually read.
Notes
  • The number of bytes read will be Size times the return value.
  • If end of file is reached (or an error occurs) before Count items are read, the return value (count of items actually read) will be less than the number requested.
  • If a file is opened for update, both reads and writes are allowed; however, they must be separated from each other by an intervening seek or rewind.
  • The type size_t is defined in stdio.h.
Example #include <stdio.h> . . . FILE * Temperatures; int Temperature [24]; size_t Count; int i; . . . Count = fread (Temperature, sizeof (int), (size_t) 24, Temperatures); for (i = 0; i < (int) Count; ++ i) { printf ("Temperature %i = %i.", i, Temperature [i]); }
Related functions
fopen fclose fread fwrite fseek rewind ftell

c/c++ Index