Getting PENS from std :: ofstream

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

This is the opposite of this question:

Can I use CreateFile, but force the handle to std :: ofstream?

For this reason, I want to change the attributes of a file (for example, creation time) without opening the file with CreateFile.

+3
source share
6 answers

++ ofstream, , . streambuf, HANDLE, ostream, . , , , .

+4

" Unix, Windows". , , . . , .

-, _Filet * fdbuf. , , . , fstream, filebuf, ( "_Filet * _Myfile;" ):

 friend HANDLE __HACK_getFilebufHANDLE(filebuf*);

. , :

namespace std {
   HANDLE __HACK_getFilebufHANDLE(filebuf*in) {
      return (HANDLE) _get_osfhandle(_fileno(in->_Myfile));
   }
};

, , , rdbuf (iobuf, filebuf). , " " , ( , , ):

  __HACK_getFilebufHANDLE((filebuf*)fopoutstrm.rdbuf())

, .

+3

. FILE* ( _Filet*, ) std::basic_filebuf.

+1

++. , Boost.IOStreams . Device, boost::iostreams::stream_buffer<> , boost::iostreams::stream<>.

+1

Visual++ 2010 . , Visual++ 2005, :

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);
0

. . : "std::ifstream ifs(fh);" msv, , 2008 .

I find another way, you can list the descriptor in your process and find the descriptor that returns the file name.

So I get a pen.

0
source

Source: https://habr.com/ru/post/1789942/


All Articles