View Full Version : fast partial file access
WarpEnterprises
14th December 2004, 14:10
I'm trying to read only the headers of an mp3 file: read 40bytes, calculate the offset to the next header (~400bytes), seek to the new position and do it again over and over.
Which is the fastest way to do so?
Reading the whole file into mem won't be, because only about 10% of data has to be read.
Using fopen,... seems to buffer the whole file.
Any ideas?
maven
14th December 2004, 14:31
Map the file into memory.
WarpEnterprises
14th December 2004, 14:50
How exactly is this done? I suppose this is something different than "fread" in one chunk into my mem?
And isnt't any advantaged spoiled by the fact that the mapping would read 4K or so blocks (in my case the entire file because the distance between two headers is << 4K)
jsoto
14th December 2004, 23:51
What I use is fread + fseek in a loop, but I'd like to know if there is a faster way
jsoto
hank315
15th December 2004, 01:20
And isnt't any advantaged spoiled by the fact that the mapping would read 4K or so blocks (in my case the entire file because the distance between two headers is << 4K)In such a case you will always read the entire file, the gaps you're not interested in are just too small.
In general, the fastest way to read a file is to open it in binary mode and read blocks which are just as long as the buffers used by the OS. But I'm not used to program in C and don't know if this is even possible in C.
maven
15th December 2004, 12:17
Originally posted by WarpEnterprises
How exactly is this done? I suppose this is something different than "fread" in one chunk into my mem?
And isnt't any advantaged spoiled by the fact that the mapping would read 4K or so blocks (in my case the entire file because the distance between two headers is << 4K)
CreateFileMapping, OpenFileMapping, MapViewOfFile(Ex), UnmapViewOfFile.
Any possible advantage is "spoiled" as you put it because of read-ahead caches anyway (in the HDD if contiguous and by the OS in any case). Data is read in larger chunks anyway...
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.