Getting a HANDLE from a std::ofstream

后端 未结 6 1719
天涯浪人
天涯浪人 2021-01-18 14:34

Is it possible to get the underlying file HANDLE from a std::ofstream (Visual C++ 2005)?

This is the opposite of this question:

Can I use CreateFile, but for

6条回答
  •  悲哀的现实
    2021-01-18 15:15

    With the VisualC++ 2010 libraries, the following should work. I assume it's the same for VisualC++ 2005, but you will have to verify:

    FILE* fh = fopen(...);
    
    HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fh));
    // do something on hFile
    
    // create iostream from FILE
    std::ifstream ifs(fh);
    
    // use it...
    
    // close FILE
    _close(fh);
    

提交回复
热议问题