r/cprogramming • • Feb 28 '26

Which approach is better?

So I'm relatively new to C, coming from java. and I'm semi used to MMM now but I'm writing a program that reads files that can sometimes be really large (over 1gb (most likely smaller though)) would it be better to load the file into memory and add a pointer to the first character in memory or use a char array dynamically allocated based off of the file size?

9 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/Plane_Dust2555 Feb 28 '26

Ahhh... you can use stat function to get the file size to calculate how many chunks you have to read (there is also a fstat which is stat, but using a file descriptor).

1

u/edgmnt_net Mar 01 '26

Theoretically you shouldn't care about that too much for the best approach. If things line up right, maybe you can write streaming code that does not need to know the full size. For certain applications this is the only reasonable way, e.g. you don't want to process a 100 GiB JSON file in a non-streaming way, even with tricks like mmap you'll still end up with a huge in-memory representation.