Why can't I open a file for reading if (theoretically) I should be allowed?
c, file-io, winapi
Solution
Your CreateFile() call explicitly denies write sharing, you specified FILE_SHARE_READ. That cannot work, the first program already gained write access since it used GENERIC_WRITE. You cannot deny a right that was already acquired so the call will fail with a sharing violation error.
To make it work, the second call will have to specify FILE_SHARE_WRITE instead. And deal with the headache of trying to read from a file that's being written to at unpredictable times and places. This typically only comes to a good end when the 1st process only appends to the file and doesn't seek. And you properly dealing with sometimes only getting a part of the appended data because some of it is still stuck in a buffer or in the process of being written. Tricky stuff. Consider a pipe in message mode if that's a problem.
Reiterating from the comments, the sharing flags do not control what you can do, they control what another process can do with the file. What you want to do is specified in the 2nd argument. So the missing FILE_SHARE_WRITE is the problem since it prevents another process from writing to the file. But it already does.
Problem
I have two projects in C: The first: ``` include windows.h include stdio.h include tchar.h int main() { HANDLE hFile = CreateFile("D:\\f.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) _tprintf("Error: CreateFile %d\n",GetLastError()); Sleep(5000); return 0; } ``` The Second: ``` include windows.h include stdio.h include tchar.h int main() { HANDLE hFile = CreateFile("D:\\f.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) _tprintf("Error: CreateFile %d\n",GetLastError()); return 0; } ``` The first program is supposed to open the file for reading while allowing others to read from it. The second is supposed to open the file for reading. When I run the program, the second one give me error 32 (ERROR_SHARING_VIOLATION). I thought the whole point of FILE_SHARE_READ was to allow other threads/processes to open a file just for reading regardless of whether it's already open or not. Can anyone help me solve this problem? P.S. If the file were a mailslot, would that make any difference?