how to handle open() errors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fabrizio

    #1

    how to handle open() errors?

    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"<<end l;
    }

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

    Thanks.

  • John Harrison

    #2
    Re: how to handle open() errors?

    fabrizio wrote:[color=blue]
    > 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"<<end l;
    > }
    >
    > How can I handle the error? Essentially, how can I print the error to
    > know the reason of it?
    >
    > Thanks.
    >[/color]

    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

    Comment

    Working...