views:

122

answers:

1

I have two HANDLEs and they are created from the same file,

in such condition I want to write on offset from 1 to 100 using the first HANDLE,

and from 101 to 200 using the 2nd HANDLE, from 201 to 300 using the first HANDLE,

...,

How can I make this operation seems like a sequential write and no time is wasted

between positioning the the pointers in the HANDLE?

+1  A: 

You should be able to do asynchronous overlapped IO.

To get you started, look at the WriteFile win32 API call. It discusses how to use CreateFile with the FLAG_FILE_OVERLAPPED flag. You then call WriteFile and pass in an OVERLAPPED parameter, which contains the offset to start writing at and an event handle, which gets signaled when the IO is complete.

Alternativally, you can call WriteFileEx, which calls a function that you supply when the IO is complete, rather than signaling an event.

Note that you should write in blocks of 4K (4096) bytes rather then in blocks of 100 bytes, since this is the size of page files in Windows; it will speed up your IO considerably. Also note that this should only require one file handle, rather than multiple.

James Hugard
You'll also need to give some thought to canceling and exception handling, which can get pretty hairy on async IO since you need to keep track of all pending operations to properly shut things down.Too bad you can't write this in F#... it makes this kind of code drop-dead easy, since async code with callbacks is written in a linear style, and things like exception handling and cancel pending are done by the library.
James Hugard
Of course if you can't write in 4K blocks then memcpy'ing into the appropriate place in a memory buffer and then async writing the whole block in a one'r is the way forward.
Goz