I wanted to write a function like this that would return a buffer containing a binary file:
Code:
char *getbin (char *filename) {
char *buffer;
struct stat finfo;
FILE *fstRO = fopen(filename, "rb");
if (fstRO == NULL) return NULL;
stat(filename,&finfo);
buffer=ec_malloc(finfo.st_size);
if (fread(buffer,1,finfo.st_size,fstRO) != finfo.st_size) {
fclose(fstRO); return NULL;}
fclose(fstRO); return buffer;
}
It works fine -- the only problem is it's impossible to determine how big the returned buffer is! strlen() returns the wrong number (hitting a 0, i guess). Do I have to change the function to return a typedef struct containing the buffer and the size, or is there some way to measure a buffer containing binary data?