473,492 Members | 4,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help with standard streams and the file descriptor

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/libstd.../howto.html#11

Help is appreciated.
Thanks,
Robert
Jul 22 '05 #1
1 1365
On Mon, 05 Jan 2004 20:08:40 -0500, "Robert Schweikert"
<rj******@cox.net> wrote in comp.lang.c++:

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/libstd.../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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9724
by: Joe | last post by:
Hi, I want to use the C library function mkstemp. It returns a unix file descriptor (int) to an open file. How do I convert the file descriptor to a valid ofstream object? Thanks, Joe
6
1760
by: Siddharth Taneja | last post by:
Hi, I have a very simple prg over here, trying to read the lines of a file #include <iostream> #include <fstream> #include <iostream> #include <string> using namespace std;
3
7055
by: gipsy boy | last post by:
Given a file descriptor (from a network socket, for instance), I want to have an iostream that reads/writes to it, so I can push and read data in the traditional way : sockStream << "<some...
2
2454
by: Thorsten Knopel | last post by:
Dear all, Because of some reason, I must get a file descript from a c++ I/O stream, for example: ifstream. Under OpenUnix I have a function ifstream::fd (rdbuf->fd) to do this. But in Linux,...
4
5974
by: lynology | last post by:
I need help trying to figure why this piece of code gives me a "Bad File descriptor error" everytime I try to run it and invoke fflush. This piece of code simple outputs a char string to the output...
4
4548
by: darrin.skinner | last post by:
Hi all, How can I open a file/pipe to something other than STDOUT/STDERR? I.e. I want to write to file descriptor # 3. fopen("php://std3"... does not work. fopen("/dev/ttyS3"... does not...
3
1811
by: akaarj | last post by:
I am using Linux gcc 4.1.1 version I have a file descriptor and I want to have a FILE* pointer to the file from the file descriptor. OR if I can have the file name using the file descriptor.....
5
5237
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file...
9
9276
by: Bill David | last post by:
I know it's very strange to do that since we have the file name when we call: int open(const char *pathname, int oflag,...); And we can store the file name for later usage. But I just wonder if...
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.