How to check if a file has been opened by another application in C++?

后端 未结 10 1825
耶瑟儿~
耶瑟儿~ 2020-12-02 01:55

I know, that there\'s the is_open() function in C++, but I want one program to check if a file hasn\'t been opened by another application. Is there any way to d

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 02:26

    Perhaps you could just try and get a full write lock? It'll fail if anyone else has it open for reading or writing.

    fopen("myfile.txt", "r+")
    

    If it's not cross platform and is Win32, then you can request even more fine-grained set of locks.

    See here and look at dwShareMode, value of 0, as well as the other parameters.

提交回复
热议问题