I am trying to write a single function that will read a certain number of bytes from a C file, or the entire file, based on the arguments. I want it to be memory-safe of course, and deal with NULL
terminators and the like. Not only that, but it should be efficient.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read(FILE *file, long bytes) {
/* This function takes a FILE pointer <file> and reads <bytes> bytes from it. These
* bytes are returned as a char* back to the calling function and should be freed
* since they were malloc'd. This function was an attempt to recreate the
* .read( [bytes] ) method on python file objects */
/**********************************
Set bytes to 0 to read entire file
**********************************/
long fileSize = 0; // File size to zero before byte check
if (bytes == 0) {
fseek(file, 0L, SEEK_END); // seek to end
fileSize = ftell(file); // find size of file by checking end
rewind(file); // back to the beginning for reading
} else {
fileSize = ++bytes; // extra byte for NULL
}
char buffer[16]; // We will read the file 16 bytes at a time
char *file_out = malloc(fileSize + 1);
if (file_out == NULL) {
fputs("Could not allocate enough memory", stderr);
exit(EXIT_FAILURE);
}
while (fgets(buffer, 16, file) != NULL) {
strncat(file_out, buffer, 16);
}
return file_out;
}
Is this efficient enough for large files and fringe cases?
fread()
. \$\endgroup\$read
; if it does, it is a lower-level operation thanfread
. Because this function is widely known and used, naming your functionread
is likely to confuse people who need to read your code in the future. \$\endgroup\$