Luca Luca - 1 month ago 40
C++ Question

WriteFile overlapped and fwrite equivalent

On Windows, the

function has a parameter called
which lets you specify an offset at which to write to the file.

I was wondering, is there is an
cross-platform equivalent of that?

I see that if the file is opened with the
flag, I might be able to use
to write to a particular offset. My question is - will this approach be equivalent to the overlapped
, and will it produce the same behaviour on all platforms?


The reason I need this is because I am writing blocked compressed data streams to a file, and I want to be able to load a specific block from the file and be able to decompress it. So, basically if I keep track of where the block begins in a file, I can load the block and decompress it in a more efficient manner. I know that there are probably better ways to do this, but I need this solution for some backwards compatibility.


Assuming you are okay with using POSIX functions and not just things from the C or C++ standard libraries, the solution is pwrite (aka: positioned write).

ssize_t rc = pwrite(file_handle, data_ptr, data_size, destination_offset);