help with standard streams and the file descriptor

  • Thread starter Robert Schweikert
  • Start date
R

Robert Schweikert

Hi,

I am trying to get uses of iostream up to par with the new
iostream model, i.e. standard C++ I/O streams. I have run into
some problems and was able to work around most of them. However,
for this one particular problem I am at a loss and am hoping
someone on the list can provide me with a suggestion on how to
address the issue.

The basic issue is the manipulation of flags on the file
descriptor. Currently this is implemented like so.

bool utl_noInheritFile(int fildes)
{
#if !defined (WIN32)
int flags = fcntl(fildes, F_GETFD);
flags |= FD_CLOEXEC;
return (fcntl(fildes, F_SETFD, flags) != -1);
#else
long int h = _get_osfhandle(fildes);
if (h != -1) {
HANDLE handle = reinterpret_cast<HANDLE>(h);
if (SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0))
return true;
}
return false;
#endif
}


and is called as follows:

utl_noInheritFile(journalStream->rdbuf()->fd());

The C++ standard does not support this as fd() is gone. Is there
a way to do this in a portable fashion or will I be able to make
this work only if I use the gcc extension as described in

http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#11

Help is appreciated.
Thanks,
Robert
 
J

Jack Klein

On Mon, 05 Jan 2004 20:08:40 -0500, "Robert Schweikert"

Your subject line mentions "file descriptor". There is no such thing
defined or supported by C++, or C for that matter.
Hi,

I am trying to get uses of iostream up to par with the new
iostream model, i.e. standard C++ I/O streams. I have run into
some problems and was able to work around most of them. However,
for this one particular problem I am at a loss and am hoping
someone on the list can provide me with a suggestion on how to
address the issue.

The basic issue is the manipulation of flags on the file
descriptor. Currently this is implemented like so.

bool utl_noInheritFile(int fildes)
{
#if !defined (WIN32)
int flags = fcntl(fildes, F_GETFD);
flags |= FD_CLOEXEC;
return (fcntl(fildes, F_SETFD, flags) != -1);
#else
long int h = _get_osfhandle(fildes);
if (h != -1) {
HANDLE handle = reinterpret_cast<HANDLE>(h);
if (SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0))
return true;
}
return false;
#endif
}


and is called as follows:

utl_noInheritFile(journalStream->rdbuf()->fd());

The C++ standard does not support this as fd() is gone. Is there
a way to do this in a portable fashion or will I be able to make
this work only if I use the gcc extension as described in

http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#11

Help is appreciated.
Thanks,
Robert

Neither C nor C++ ever supported file descriptors. These are
non-standard extensions provided by particular compiler/OS
combinations. You need to ask this in a group supporting the
compiler/OS combination(s) involved, it is not a language issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top