I have to do a program (for Linux) where there’s an extremely large index file and I have to search and interpret the data from the file. Now the catch is, I’m only allowed to have x-bytes of the file cached at any time (determined by argument) so I have to remove certain data from the cache if it’s not what I’m looking for.
If my understanding is correct, fopen (r) doesn’t put anything in the cache, only when I call getc or fread(specifying size) does it get cached.
So my question is, lets say I use fread and read 100 bytes but after checking it, only 20 of the 100 bytes contains the data I need; how would I remove the useless 80 bytes from cache (or overwrite it) in order to read more from the file.
EDIT By caching I mean data stored in memory, which makes the problem easier
fread‘s first argument is a pointer to a block of memory. So the way to go about this is to set that pointer to the stuff you want to over write. For example lets say you want to keep bytes 20-40 and overwrite everything else. You could either a) invokefreadon start with a length of 20 then invoke it again onbuffer[40]with a size of 60. or b) You could start by defragmenting (ie copy the bytes you want to keep to the start) then invokefreadwith a pointer to the next section.