write 或 read 函数总是删除除第一个字母之外的所有内容。有谁知道为什么?我有一个用管道通信的父亲和一个孩子。我在将它放入 write 之前检查了 tha 变量,它没有被删除。
#include<dirent.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
int main(int argc, char *argv[])
{
time_t tm_now;
struct tm *ptm_now;
time(&tm_now);
ptm_now = localtime(&tm_now);
int chanal_father[2];
int chanal_child[2];
pipe(chanal_father);
pipe(chanal_child);
char message_child[50];
char message_father[50];
char message_return[50];
if (fork()==0)
{
read(chanal_father[0], message_father, strlen(message_father)+1);
if(strcmp(message_father, "day") == 0) {
int day = ptm_now->tm_mday;
int month = (ptm_now->tm_mon)+1;
int year = (ptm_now->tm_year)-1900;
sprintf(message_return, "%2d.%2d.%2d", day, month, year);
}
else {
sprintf(message_return, "unknown function!");
}
write(chanal_child[1],message_return, strlen(message_return)+1);
exit(0);
}
write(chanal_father[1], argv[1], strlen(argv[1])+1);
read(chanal_child[0], message_child, strlen(message_child)+1);
printf("%s\n", message_child);
}