Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top