If you use libc stdio, you can use fseek and ftell to allocate the right size in one go and read in the buffer.