Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: reading files which are > 4 GB

Tags:

c

file

freebsd

I have some kind of reader which only has a handle (FILE*) to a file. Another process keeps writing to a the same file which I don't have control.

Now, as the other process appends images to that file, it is likely that soon the file size will cross 4 GB limit.

The reader process reads that file using the handle, offset and length of the image file which can be found from some DB.

My question is how would reader be able to read the chunk from the file which will be present after 4GB size.

I'm working on Win32 machine.

EDIT: I'm working on FreeBSD machine as well.

like image 361
Mayank Avatar asked Jan 19 '23 07:01

Mayank


1 Answers

Just use the standard C API on Windows, fread, fwrite work just fine on large files. You will need _fseeki64 to seek to a 64-bit position.

You can alternatively use the plain WinAPI (ReadFile, etc.) which can also deal with >4 GiB files without problems.

[Edit]: The only thing you really need is a 64-bit seek, which ReadFile provides via the OVERLAPPED structure (as some commenters mentioned.) You can of course also get by using SetFilePointer which is the equivalent of _fseeki64. Reading/Writing is never a problem, no matter the file size, only seeking.

like image 152
Anteru Avatar answered Jan 29 '23 13:01

Anteru