开发者

Opening the same file twice with different flags?

Can I open the same file twice (with CreateFileA), using different flags (in this case, one with FILE_FLAG_NO_BUFFERING, and one without)?

In detail, this is the case: During startup, I create a temporary file (with FILE_FLAG_DELETE_ON_CLOSE). I fill it up sequentially, and I don't want to worry about doing unbuffered IO in this part. Then, while the process is running, I want to access that file using unbuffered IO, because I have my own caching logic. Thus, I'm thinking of opening the same file again, this time with FILE_FLAG_NO_BUFFERING, and then closing the old handle. I want to开发者_开发问答 do this in this overlapped way for two reasons:

  1. Concurrency. If I close the old handle before I open the new one, someone else might mess with my file in the meantime.
  2. FILE_FLAG_DELETE_ON_CLOSE would delete my file when I close the first handle without having another one open. This is a minor annoyance that I could work around.


Just remember to include FILE_SHARE_DELETE in share mode. I think FILE_FLAG_DELETE_ON_CLOSE is the only flag that affects more than just "your" handle.


Isn't it ridiculous?

You want to open twice because if open after close previous handle someone might mess with your file. But reality is that you are trying to mess with your file.

If can not guarantee exclusive file access how can you prevent someone doing something? But if you open exclusively how you can reopen the file?

AFAIK, if the file is already opened exclusively no more open is allowed, even from the same process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜