C file input/output: Difference between revisions

Content deleted Content added
Functions: Added content
Tags: Mobile edit Mobile web edit
It is not an error to read less than 5 bytes, so handle it gracefully
Line 247:
int main(void) {
char buffer[5];
size_t len;
FILE* fp = fopen("myfile", "rb");
 
Line 254 ⟶ 255:
}
 
if ((len = fread(buffer, 1, 5, fp)) < 50) {
fclose(fp);
fputs("An error occurred while reading the file.\n", stderr);
Line 263 ⟶ 264:
 
printf("The bytes read were: ");
for (int i = 0; i < 5len; ++i) {
printf("%02X ", buffer[i]);
}