问题
Is it possible to create an ofstream object from a given stdio file (old-style C file handle)? Preferably without closing and re-opening the file.
The purpose for this is I have a stdio file and a library I'm using takes a stream object.
回答1:
If using boost is an option, and the library takes std::basic_ostream
(as opposed to an std::ofstream
) you could use a File Descriptor Sink to wrap your file descriptor, construct a stream from of it, and pass it to your target library.
来源:https://stackoverflow.com/questions/8073846/construct-ofstream-from-stdio-file