There are a couple of approaches you can take:
- specify a maximum size that you can handle, then you just allocate once (whether as a global or on the heap).
- handle the file in chunks if you're worried about fitting it all into memory at once.
- handle an arbitrary size by using
malloc with realloc (as you read bits in).
Number 1 is easy:
static char buff[900001]; // or malloc/free of 900000
count = fread (buff, 1, 900001, fIn);
if (count > 900000) // problem!
Number 2 is probably the best way to do it unless you absolutely need the whole file in memory at once. For example, if your program counts the number of words, it can sequentially process the file a few K at a time.
Number 3, you can maintain a buffer, used and max variable. Initially set max to 50K and allocate buffer as that size.
Then try read in one 10K chunk to a fixed buffer tbuff. Add up the current used and the number of bytes read into tbuff and, if that's greater than max, do a realloc to increase buffer by another 50K (adjusting max at the same time).
Then append tbuff to buffer, adjust used, rinse and repeat. Note that all those values (10K, 50K and so on) are examples only. There are different values you can use depending on your needs.