This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Accessing the underlying file descriptor in fstream



About a year ago I asked about if libstdc++ will provide a way to access
the underlying file descriptor in the fstream class.  Martin replied
saying I could use fstream::rdbuf()->fd().  When I tried to compile my
code with the new gcc-3.0 branch I discovered the method is not there.  I
need to access this method so that I can use posix file locks.  I release
that this is not in the C++ standard, but sense all file io on a Unix
system must be implemented on top of file descriptors there ought to me a
way to get to it.  I am also not interested in using a totally separate
fstream class as that is highly unlikely to work with Egcs and Gcc 2.95
both which I would like my program to be able to compile under for the
next couple of years.

So my question is: Will I be able to get to the underlying file descriptor
in Gcc 3.0?

The relevant part of the previous conversion follows:

Date: Sun, 16 Jan 2000 17:06:17 -0500
From: Kevin Atkinson <kevinatk@home.com>
To: libstdc++@sourceware.cygnus.com
Subject: C++ Streams and the underlying file descriptor

Although not strictly defined by the C++ standard on almost all POSIX
system all Filo IO has to done on top of file descriptors.  However the
C++ standard defines NO way of getting to the underlying descriptor.
However, in my view, there ought to be a standard way of accessing the
underlying file descriptor and creating a C++ stream from an existing
descriptor on all POSIX systems.  Will the new libstdc++ provide such a
way?

I release I can create easily create a custom streambuf which uses read
and write or even the higher level fread and fwrite.  However this is
bound not to be nearly as fast as the standard C++ streams.

I release that C++ streams do not necessarily have to written on top of
C *streams* however on unix systems they basically have to written on
top of *file descriptors*.  Because gcc basically is primary designed
for unix and unix like systems, there ought to be a standard way of
accessing the underlying file descriptor and creating a C++ stream from
an existing descriptor.

So will the new libstdc++ provide such a way?


Date: Mon, 17 Jan 2000 00:37:50 +0100
From: Martin v. Loewis <martin@loewis.home.cs.tu-berlin.de>
To: kevinatk@home.com
Cc: libstdc++@sourceware.cygnus.com
Subject: Re: C++ Streams and the underlying file descriptor

> So will the new libstdc++ provide such a way?

If you compile the current CVS libstdc++ with _STREAM_COMPAT, you get
a filedesc() method on an fstream object, returning the file
descriptor. Alternatively, you can use fstream::rdbuf()->fd()
(which filedesc() is a short-hand for).

Regards,
Martin


Date: Sun, 16 Jan 2000 19:05:40 -0500
From: Kevin Atkinson <kevinatk@home.com>
To: Martin v. Loewis <martin@loewis.home.cs.tu-berlin.de>
Cc: libstdc++@sourceware.cygnus.com
Subject: Re: C++ Streams and the underlying file descriptor

"Martin v. Loewis" wrote:
>
> > So will the new libstdc++ provide such a way?
>
> If you compile the current CVS libstdc++ with _STREAM_COMPAT, you get
> a filedesc() method on an fstream object, returning the file
> descriptor. Alternatively, you can use fstream::rdbuf()->fd()
> (which filedesc() is a short-hand for).

Thanks.  Sorry Ithat I didn't look at the code closer.  I have two
questions though.

1) Is the filedesc() and or fstream::rdbuf()->fd() methods standard
practice on POSIX systems?
2) When the new libstdc++ makes it into gcc can I expect the
_STREAM_COMPAT to be defined by default on most unix like systems?
(When taking a closer look at the iostream in shipped with gcc I noticed
these methods are there)

The reason I ask is because I need to use posix locks in one of my
program and I would like to to still be able to use C++ streams however
I don't want to unnecessary sacrifice portability.


Date: Mon, 17 Jan 2000 09:28:35 +0100
From: Martin v. Loewis <martin@loewis.home.cs.tu-berlin.de>
To: kevinatk@home.com
Cc: libstdc++@sourceware.cygnus.com
Subject: Re: C++ Streams and the underlying file descriptor

> 1) Is the filedesc() and or fstream::rdbuf()->fd() methods standard
> practice on POSIX systems?

I'm not certain SunPro CC4 has filebuf::fd(); SunPro CC 5 has it if
_RWSTD_NO_EXTENSION is not defined. I could not find another system
that has filedesc; perhaps there is no point in maintaining that
anymore.

> 2) When the new libstdc++ makes it into gcc can I expect the
> _STREAM_COMPAT to be defined by default on most unix like systems?

They are defined if you include old-style headers, like stream.h, and
they are not recommended for new code. However, filebuf::fd does not
depend on this define.

Regards,
Martin

-- 
Kevin Atkinson
kevina at users sourceforge net
http://metalab.unc.edu/kevina/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]