The file is open for reading and writing, system caching is disabled:

::CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL); 

Opened correctly, he was assigned some kind of handle. Something recorded there. The file is not closed, but we know that it will not be guaranteed to change. Is it possible to re-open this file, but only for reading?

  • Why re-open it, if it was already open for reading, and, perhaps, read? - Regent
  • one
    It seems to me to try faster than typing a question on SO. - ixSci
  • One thread has written, but not yet closed, the second thread wants to read. Save the handle and give it to the second stream can not. - sveta_t
  • So open another descriptor in the second thread, what's the problem? - ixSci
  • one
    FILE_FLAG_OVERLAPPED is generally from another opera, it doesn’t have anything to do with what you think. The name of this flag is very unfortunate. In addition, WinAPI does not throw exceptions, and if you have an exception somewhere (SEH?), The problem is not in the API call, but in some parameters. - ixSci

2 answers 2

  //Файл открыт на чтение и запись ::CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL); // второй файл открыт для чтения ::CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 

    You just had to look at the description of the API and everything becomes very clear. Open the file as follows:

     ::CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL); 

    And then just open for reading in another thread. Documentation on the function must be read, there is nothing supernatural.

    • Yes, I did it first. I cannot call CreateFile again - an error. GetLastError returns code 32 - ERROR_SHARING_VIOLATION. - sveta_t
    • @sveta_t: How do you reopen? The same code? - VladD