I need the Windows FileHandle associated with a C++ iostream using mingw on Windows. This is probably the same as the UNIX File Descriptor associated with a C++ iostream on Windows. Does anyone know how to find it? Thanks.
Asked
Active
Viewed 906 times
0
-
What do you require it for? Depending on the actual goal there may be a portable/standard answer – sehe Nov 12 '11 at 21:19
-
There's no standard way to do it, for the simple reason that an iostream might not be implemented in terms of file handles at all. If you dig into the implementation details of your library, you'll find the handle-based streamimpl and with some ugly casting you'll be able to find the reference to that in your iostream object and pull out the handle. – antlersoft Nov 12 '11 at 21:21
-
I require it so that I can flush the disk file associated with the iostream. http://stackoverflow.com/questions/8107436/how-do-i-flush-a-stdlib-output-file-on-win32 – vy32 Nov 12 '11 at 21:33
1 Answers
2
Did you see my answer, here:
How do I flush a stdlib output file on win32?
std::basic_filebuf<char> *file_buf = dynamic_cast<std::basic_filebuf<char> *>(f.rdbuf());
if (file_buf != 0) {
struct to_get_protected_member : public std::basic_filebuf<char> {
int fd() { return _M_file.fd(); }
};
printf("your fd is %d\n", static_cast<to_get_protected_member *>(file_buf)->fd());
}