read, write & STDOUT_FILENO....

hi guys, I'have a question 4 u.

Why this code give me the right output (an integer on the stdout):


read(fd,&mpid,sizeof(pid_t));
printf("%d\n",mpid);

Instead this code give me only a blank line:


read(fd,&mpid,sizeof(pid_t));
write(STDOUT_FILENO,&mpid,sizeof(pid_t));

?????

The "fd" file descriptor points to a fifo, but I think this isn't important cause I've got the same problem if i try reading from a normal file.

mpid is an integer, but when you send it though printf with the format you are using, it gets converted into a string of ascii characters. This string can be be displayed. In the second case you are sending the integer directly to stdout. If integers are 32 bits on your system once in a while it may be 3 printable characters followed by a null byte and thus look like a string. But most of the time it will be unprintable.

Thanx a lot Perderabo.