2

I am reading data sent by the parent process using pipe. Parent process close the read end and write data on the write end of pipe. Similarly, child closes write end and read data from read end.

But in my case, read returned the "-1" which is error value. How should I find that, which error(like EAGAIN, EBADF, EIO ) has been occurred in read call? Thanks

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
ajay_t
  • 2,347
  • 7
  • 37
  • 62
  • possible duplicate of [How to know what the errno means ?](http://stackoverflow.com/questions/503878/how-to-know-what-the-errno-means) – Tony Delroy Feb 28 '13 at 07:02
  • 1
    No dupe, as the question is not about what `errno`'s values mean, but where the cause of the error indicated by the `-1` returned by `read()` is stored. – alk Feb 28 '13 at 07:12

3 Answers3

4

How should i found that, which error(like EAGAIN, EBADF, EIO ) has been occurred in read call?

Print errno. An even better option is to do a perror, right after the call.

if (read(...) < 0)
    perror("read");

Or use strerror if you need to get the message yourself:

printf("%s\n", strerror(errno));

Note you'll need to #include <errno.h> if you use errno directly.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

If you use linux the following code will print a related error message as a string:

printf("%s", strerror(errno));
KBart
  • 1,580
  • 10
  • 18
2

A non-portable glibc extension, try simply: printf( "%m" );

(Glibc extension; supported by uClibc and musl.) Print output of strerror(errno). No argument is required.

Mike
  • 4,041
  • 6
  • 20
  • 37
Jeremy
  • 304
  • 1
  • 6