There's no reason to use a loop if you're just dumping the whole file into memory anyway. That is unless you want to incrementally error out in the event of using too much memory, for some reason.
Copied from
SO[
^] :
#include <stdio.h>
#include <stdlib.h>
FILE *f = fopen("textfile.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
That being said though, doing that isn't always the best practice. It's fast but you're limited by memory. It's usually better to process your file line-by-line or chunk-by-chunk. In which case, you'll need that loop again.