write的写停止问题

##例程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>

int main()
{
    char buf[100];
    strcpy(buf, "hello\nworld");
    write(STDOUT_FILENO, buf, strlen(buf));
}

输出为

hello
world(光标在这里)

int main()
{
    char buf[100];
    strcpy(buf, "hel\0lo\nworld");
    write(STDOUT_FILENO, buf, strlen(buf));
}

输出为

hel(光标在这里)

int main()
{
    char buf[100];
    strcpy(buf, "hello\nworld");
    fputs(buf, stdout);
}

输出为:

hello
world

int main()
{
    char buf[100];
    strcpy(buf, "hel\0lo\nworld");
    fputs(buf, stdout);
}

输出为:

hel(光标在这里)

int main()
{
    char buf[100];
    strcpy(buf, "hello\nworld");
    write(STDOUT_FILENO, buf, strlen(buf));
}

输出:

hello
world(光标在这里)

##总结
其余socket的数据传输函数也是如此,只以\0作为字符串的结尾,而不是\n,\n将被认为是字符进行传输,并且后面的字符也会传输,直到遇到\0为止

如果只要打印某一部分字符,则在此字符串之后一定要加\0,因为\0才被认为是一个字符串的结束符