开发者

Linux - why error message doesn't show up aftering calling write(2, "...")?

I compiled the following code with gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) under Ubuntu 10.04 LTS.

user@ubuntu:~/doc$ cat simple_write.c
#include <unistd.h>
#include <stdlib.h>

int main()
{
    if ((write(1, "Here is some data\n", 18)) != 18)
        write(2, "A write error has occur开发者_C百科red on file descriptor 1\n",46);

    exit(0);
}

user@ubuntu:~/doc$ ./simple_write 
Here is some data

Can someone explain to me why the second error message is not printed? Is it redirected to other place? Then, how to make it show up?

Thank you


The second message doesn't show up because the first write() successfully wrote 18 characters.

To demonstrate, change != into ==.


The return value from write is:

Upon successful completion, these functions shall return the number of bytes actually written to the file associated with fildes.

You asked it to write 18 bytes and it did so it returns 18. Your error won't be printed because, well, 18 != 18 is false. Maybe you're confused because \n is actually a single byte on Unixy systems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜