Why I'm getting trash on this pipe?

橙三吉。 提交于 2019-12-01 19:57:53

Did you mean:

rdbuf[numread] = '\0';

buf in fd_server.c has the same problem.

This:

buf[numread] = '0';

is wrong. You want:

buf[numread] = '\0';

(Same with rdbuf[numread] = '0';.)

These lines produce bad output:

buf[numread] = '0';
printf("Full Duplex Server : Read From the pipe : %s \n", buf);

First, buf[numread] = '0'; Overwrites your null-terminator.
With this overwritten, printf(%s) doesn't know where to stop printing.

The null-terminator tells C where the string ends.
After you overwrote it, C no longer knows where the end of the string is, and prints your string "HAHAHA", but keeps printing garbage after that.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!