C: reading files> 4 GB in size

I have some kind of reader that only has a file descriptor (FILE *). Another process continues to write to the same file that I do not have.

Now that another process is adding images to this file, it is likely that soon the file size will exceed the 4 GB limit.

The reading process reads this file using the descriptor, offset, and length of the image file, which can be found from some database.

My question is how can the reader read a piece from a file that will be present after 4 GB.

I am working on a Win32 machine.

EDIT: I am also working on a FreeBSD machine.

+4
source share
2 answers

In the FreeBSD API, stdio is not limited to 32 bits (4Gb).

You should have no trouble reading in 4Gb if you use a 64-bit integer to handle offsets and lengths.

If you are looking in FILE *, you will need to use fseeko (), not fseek () if you are using a 32-bit host. fseek () takes up a long 32-bit bit on 32-bit machines. fseeko () accepts the type off_t , which is 64 bits on all FreeBSD architectures.

+2
source

Just use the standard C API for Windows, fread , fwrite work great with large files. You will need _fseeki64 to search for a 64-bit position.

Alternatively, you can use a simple WinAPI ( ReadFile , etc.), which can also work with files> 4 GiB without problems.

[Edit]: The only thing you really need is a 64-bit search, which ReadFile provides through the OVERLAPPED structure (as some commentators have noted). Of course, you can also use SetFilePointer , which is equivalent to _fseeki64 . Reading / writing is never a problem, regardless of file size, only for searching.

+5
source

All Articles