how to handle open() errors?

F

fabrizio

Opening /dev/tap0, I have an error in fact open() returns -1.

int fd = open("/dev/tap0",O_RDWR);
if (fd == -1)
{
cerr<<"error opening interface"<<endl;
}

How can I handle the error? Essentially, how can I print the error to
know the reason of it?

Thanks.
 
J

John Harrison

fabrizio said:
Opening /dev/tap0, I have an error in fact open() returns -1.

int fd = open("/dev/tap0",O_RDWR);
if (fd == -1)
{
cerr<<"error opening interface"<<endl;
}

How can I handle the error? Essentially, how can I print the error to
know the reason of it?

Thanks.

perror will print an error message to the standard error stream

int fd = open("/dev/tap0",O_RDWR);
if (fd == -1)
{
perror("error opening interface");
}

There are of coure no guarantees that the error message will be helpful.

john
 

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
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top