19

Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx , but then force that handle into a std::ofstream?

The interface to ofstream is obviously platform independent; I'd like to force some platform dependent settings in 'under the hood' as it were.

Steve Folly
  • 7,437
  • 8
  • 44
  • 56

2 Answers2

29

It is possible to attach a C++ std::ofstream to a Windows file handle. The following code works in VS2008:

HANDLE file_handle = CreateFile(
    file_name, GENERIC_WRITE,
    0, NULL, CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL, NULL);

if (file_handle != INVALID_HANDLE_VALUE) {
    int file_descriptor = _open_osfhandle((intptr_t)file_handle, 0);

    if (file_descriptor != -1) {
        FILE* file = _fdopen(file_descriptor, "w");

        if (file != NULL) {
            std::ofstream stream(file);

            stream << "Hello World\n";

            // Closes stream, file, file_descriptor, and file_handle.
            stream.close();

            file = NULL;
            file_descriptor = -1;
            file_handle = INVALID_HANDLE_VALUE;
        }
}

This works with FILE_FLAG_DELETE_ON_CLOSE, but FILE_FLAG_WRITE_THROUGH may not have the desired effect, as data will be buffered by the std::ofstream object, and not be written directly to disk. Any data in the buffer will be flushed to the OS when stream.close() is called, however.

ChrisN
  • 15,795
  • 8
  • 53
  • 75
  • Excellent (also works in VS2005)! One scenario I have is for writing some accounting information to a file on a USB with write-through. Because it'll be a quick open-write-close sequence, the call to close should ensure ofstream's buffer is flushed to the OS, right? – Steve Folly Jan 24 '09 at 13:42
  • Any idea on how to do the same for `fstream` instead of `ofstream`? – sorin Oct 07 '13 at 12:48
  • I've just tried this in VS2015 and it works with one important caveat. When using the undocumented constructor `std::ofstream(FILE*)`, you have to call `ofstream::close()` explicitly to close the file. The destructor of the `ofstream` object will not close it. – GetFree Apr 02 '19 at 00:39
2

Some of these flags are also available when using _fsopen / fopen:

FILE* pLockFile = _fsopen(tmpfilename.c_str(), "w", _SH_DENYWR );
if (pLockFile!=NULL
{
   // Write lock aquired
   ofstream fs(pLockFile);
}

Here we open the file so when doing a flush, then it writes through (And it is deleted when closed):

FILE* pCommitFile = fopen(tmpfilename.c_str(), "wcD");
if (pCommitFile!=NULL)
{
   // Commits when doing flush
   ofstream fs(pCommitFile);
}
Rolf Kristensen
  • 12,232
  • 1
  • 41
  • 52