You could always do this:
Code:
{
char *buffer = malloc(10), *tmp;
unsigned int n_allocs = 1;
size_t items_read;
while(fread(buffer + ((n_allocs - 1) * 10), 1, 10, in) == 10)
{
n_allocs++;
tmp = realloc(buffer, n_allocs * 10);
if(!tmp)
{
// Handle memory allocation error
}
else
buffer = tmp;
}
// Do stuff with buffer
free(buffer);
}
That will read in the entire file 10 bytes at a time. The most space you'll be wasting is sizeof(*buffer) * 10 bytes. The number doesn't have to be 10. You can make it whatever you want, but the lower the number the more freads() there will be. And the more freads() there are, the less efficient it will be. But if you make the number higher then you'll run the risk of wasting more space.
Also, the above code could use better error checking, but it serves as an algorithm example.