20

Possible Duplicate:
Getting a FILE* from a std::fstream

I am working on Linux and file descriptors are the main model in this OS.

I was wondering whether is there any library or any way to retrieve the native Linux file descriptor starting from a C++ std::fstream.

I thought about boost::iostream since there is a class called file_descriptor but I understood that its purpose is different from the one I want to achieve.

Do you know some way to do that?

Community
  • 1
  • 1
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
  • 1
    Why do you need the file descriptor? – Philip Kendall Jul 19 '12 at 10:06
  • I am looking also to NON-Standard since I will stick to Linux platform. I found this page as well. Anyone used it?Anyhttp://www.ginac.de/~kreckel/fileno/ – Abruzzo Forte e Gentile Jul 19 '12 at 10:20
  • 12
    This question is not a duplicate. This question asks how to get a file descriptor from an iostream. A file descriptor is a low-level kernel concept. The question pointed to as the original of which this is supposedly a duplicate speaks of FILE * streams, which are not the same. – Daniel Jul 04 '14 at 05:34
  • 2
    Related (since the cited duplicate is *not* a duplicate *per se*): [What's the difference between a file descriptor and file pointer?](http://stackoverflow.com/q/2423628/608639) – jww Apr 21 '15 at 22:35
  • 3
    Personally, after performing a write, I need to determine the size of the file - frequently! `file.seekg( 0, std::ios::end ); fsize = file.tellg(); file.seekg( 0, std::ios::end ); fsize = file.tellg() - fsize;` on some filesystems can take a loooong time. Meanwhile, `fstat()` on an open file descriptor is lightning-fast. – SF. May 04 '16 at 16:12
  • I agree with OP that it's not a duplicate - file descriptors and FILE * are different. My solution is shown here: https://stackoverflow.com/a/53689083/5899976 As with the selected answer, it depends on a non-portable GCC extension. – Droid Coder Dec 09 '18 at 03:34

4 Answers4

9

You can go the other way: implement your own stream buffer that wraps a file descriptor and then use it with iostream instead of fstream. Using Boost.Iostreams can make the task easier.

Non-portable gcc solution is:

#include <ext/stdio_filebuf.h>

{
    int fd = ...;
    __gnu_cxx::stdio_filebuf<char> fd_file_buf{fd, std::ios_base::out | std::ios_base::binary};
    std::ostream fd_stream{&fd_file_buf};
    // Write into fd_stream.
    // ...
    // Flushes the stream and closes fd at scope exit.
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
4

There is no (standard) way to extract the file number from an std::fstream since the standard library does not mandate how file streams will be implemented.

Rather, you need to use the C file API if you want to do this (using FILE*).

Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29
4

There is no official way to get the private file handle of a file stream (or actualy a std::basic_filebuf), just because it should be portable and discourage use of platform-specific functions.

However, you can do ugly hack like inheriting std::basic_filebuf and from that try to pry out the file handle. It's not something I recommend though as it will probably break on different versions of the C++ library.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

There is no support of exposing file descriptor neither in standard C++ nor in libstdc++.

Artyom
  • 31,019
  • 21
  • 127
  • 215