File Discriptors in C++

  • Thread starter =?ISO-8859-1?Q?Ren=E9_Kjellerup?=
  • Start date
?

=?ISO-8859-1?Q?Ren=E9_Kjellerup?=

Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(void) {

int c_fd;
int s_fd = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in server;
char buf[80];
.... // setting up the socket;
listen(s_fd,5);
while(1) {
c_fd = accept(s_fd,0,0);
if((fork()) == 0) {
int rbuf;
close(s_fd); // child do not need to listen
do {
bzero(buf, sizeof(buf));
rbuf = read(c_fd, buf, sizeof(buf));
if(rbuf == 0) {
close(c_fd);
continue;
} else {
// misc. action that the server does upon receiving data.
// among other things using the function: write(c_fd,buf,sizeof(buf));
// on more
}
} while(rbuf !=0);
exit(0);
} else {
close(c_fd);
}
}
}
// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René
 
M

Michael Mellor

René Kjellerup said:
Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code [....]
// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René
Have a look at:
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor
 
?

=?ISO-8859-1?Q?Ren=E9_Kjellerup?=

Michael said:
René Kjellerup said:
Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code
[....]

// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René
Have a look at:
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor

Thanks for the link
looks like some realy useful stuff.
(hmm... FD streams, now that a new one for me) :)
 
?

=?ISO-8859-1?Q?Ren=E9_Kjellerup?=

Michael said:
René Kjellerup said:
Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code
[....]

// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René
Have a look at:
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor

Looked it through very hard, and I see it's for reading and writing of
FDs, though nothing about closing them when they are not needed.
How can this be done?

René
 
M

Michael Mellor

René Kjellerup said:
Michael said:
René Kjellerup said:
Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code

[....]

// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René
Have a look at:
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor


Looked it through very hard, and I see it's for reading and writing of
FDs, though nothing about closing them when they are not needed.
How can this be done?

René
The simpliest way would be:
int fd = open (....);
.....
boost::fdostream out(fd);
.....
close (fd);
// don't use out and more

It would be safer to make sure the stream object is destroyed before
closing the file descriptor.
int fd = open (....);
.....
{
boost::fdostream out(fd);
.....
}
close (fd);

Michael Mellor
 
?

=?ISO-8859-1?Q?Ren=E9_Kjellerup?=

Michael said:
René Kjellerup said:
Michael said:
René Kjellerup wrote:

Hi all,
this is my first post to 'comp.lang.c++'
and I have some trouble with File Descriptors and C++
I use gcc/g++ on a Linux Mandrake 9.2 and a OpenBSD 3.4

and when I compile this code it doesn't complain:
//--- file01.c plain C code



[....]

// --- file01.c EOF

^
|
This works, but I'd like to use C++ simpler file streams,
but when I try to compile the same code as C++ then the functions;
read(), write(), and close() are not declared.
Is it correct to assume that file descriptors are not included in C++?
Is there a header that C++ can use to include them (or just these
functions)?

I'm grateful for any info on the subject, even if it means I have to
read up on some things.

Yours René

Have a look at:
http://www.josuttis.com/cppcode/fdstream.html

Michael Mellor



Looked it through very hard, and I see it's for reading and writing of
FDs, though nothing about closing them when they are not needed.
How can this be done?

René
The simpliest way would be:
int fd = open (....);
....
boost::fdostream out(fd);
....
close (fd);
// don't use out and more

It would be safer to make sure the stream object is destroyed before
closing the file descriptor.
int fd = open (....);
....
{
boost::fdostream out(fd);
....
}
close (fd);

Michael Mellor

in addition to my first post,
I use the file descriptor for Inet data transfers between two tcp
sockets, and i'd like to use the file streams for log files and data
files.


is there a better way than declaring close, read, write functions as
external C functions?

R. Kj.
 

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

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top