Mayank
Mayank

Reputation: 5738

C: reading files which are > 4 GB

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.

Upvotes: 4

Views: 2342

Answers (2)

nos
nos

Reputation: 229342

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

You should have no problems reading past 4Gb as long as you use a 64 bit integer to manipulate the offsets and lengths.

If you're seeking in a FILE* , you'll have to use fseeko() and not fseek() if you're on a 32 bit host. fseek() takes a long which is 32 bit on 32 bit machines. fseeko() takes an off_t type which is 64 bits on all FreeBSD architectures.

Upvotes: 3

Anteru
Anteru

Reputation: 19434

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.

Upvotes: 6

Related Questions