Reputation: 8373
I am working on some legacy code which opens a file and adds binary data to the file:
std::ifstream mInFile;
#ifdef WINDOWS
miWindowsFileHandle = _sopen(filename.c_str(), O_RDONLY , SH_DENYWR, S_IREAD);
#endif
mInFile.open(filename.c_str(), std::ios_base::binary);
For some reason the code opens the file twice. Is this because _sopen is used to lock the file in windows?
If so, how come std::ifstream::open doesn't lock the file?
Is there a way to check if a windows file handle has already been closed?
Upvotes: 1
Views: 1653
Reputation: 503775
It opens twice because the first one opens it, and locks it. Then fstream opens it again (somewhat contradictory to the intent of the previous statement.)
On how to just lock the file, check this question out.
Upvotes: 2