C通过命名管道发送分配的字符串-linux
我想通过命名管道发送分配的字符串,而不是简单的字符数组 我得到了以下代码:C通过命名管道发送分配的字符串-linux,c,linux,C,Linux,我想通过命名管道发送分配的字符串,而不是简单的字符数组 我得到了以下代码: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> int main() { int pid, fd; fd = mkfifo("fifo.ftc", S_IRUSR | S_IWUSR);
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
int pid, fd;
fd = mkfifo("fifo.ftc", S_IRUSR | S_IWUSR);
pid = fork();
char* send, * recieve;
if(pid == 0) {
recieve = malloc(100);
fd = open("fifo.ftc", O_RDONLY);
read(fd, recieve, sizeof(recieve));
close(fd);
printf("%s\n", recieve);
send = malloc(100);
send = "This text was sent by child!";
fd = open("fifo.ftc", O_WRONLY);
write(fd, send, sizeof(send));
close(fd);
free(recieve);
free(send);
} else if(pid > 0) {
send = malloc(100);
send = "This text was sent by parent!";
fd = open("fifo.ftc", O_WRONLY);
write(fd, send, sizeof(send));
close(fd);
recieve = malloc(100);
fd = open("fifo.ftc", O_RDONLY);
read(fd, recieve, sizeof(recieve));
close(fd);
printf("%s\n", recieve);
unlink("fifo.ftc");
free(send);
free(recieve);
}
}
看起来它没有打印出整个字符串。如何接收来自家长和孩子的全部消息?您的代码中存在多个错误
sizeof
运算符。当你做char*foo;富氏
,结果大小不是foo
所指字符串的大小,而是字符指针类型的大小(64位体系结构中8个字节,32位中4个字节)。你应该改用strlen(foo)您的代码中有多个错误
sizeof
运算符。当你做char*foo;富氏
,结果大小不是foo
所指字符串的大小,而是字符指针类型的大小(64位体系结构中8个字节,32位中4个字节)。你应该改用strlen(foo)@jweyrich已经回答了你问题的要点。然而,还有一些其他的方面你可以考虑…p> 在开始使用该语言时,C中的字符串赋值有时是一个混淆点,因为有时会出现如下赋值:
send = "This text was sent by parent!";
当它不起作用时,它会起作用。它在这种情况下起作用,因为代码中的变量send
是作为指针创建的。请记住,如果它是作为数组创建的,那么使用=
运算符进行赋值的唯一时间应该是在初始化期间。对C字符串的字符数组
形式的初始化后赋值必须以不同方式进行。通常,字符串函数(strcpy
、strcat
、sprintf
等)用于修改字符串或对字符串进行赋值;示例:
//string literal used to initialize
char send[100] = "This text was sent by parent!"; //works
//initialized with nul terminator
char send[100] = '\0';
//post initialization statements:
strcpy(send, "This text was sent by parent!");//works
strcat(send, "This text was sent by parent!");//works
sprintf(send, "%s", "This text was sent by parent!");//works
但是:
此外,建议您始终检查malloc()
的返回,以确保确实分配了内存:
send = malloc(100);
if(!send) return -1; //check return of malloc
send = "This text was sent by parent!";
还有一点:因为recieve
是一个指针,它的大小将是包含其地址所需的字节数,对于32位目标可能是4字节,对于64位目标可能是8字节。因此,声明:
read(fd, recieve, sizeof(recieve));
很可能是告诉read读取4或8个字节,这可能不是您所期望的。将此行更改为:
read(fd, recieve, 100);
@jweyrich已经回答了你问题的要点。然而,还有一些其他的方面你可以考虑…p> 在开始使用该语言时,C中的字符串赋值有时是一个混淆点,因为有时会出现如下赋值:
send = "This text was sent by parent!";
当它不起作用时,它会起作用。它在这种情况下起作用,因为代码中的变量send
是作为指针创建的。请记住,如果它是作为数组创建的,那么使用=
运算符进行赋值的唯一时间应该是在初始化期间。对C字符串的字符数组
形式的初始化后赋值必须以不同方式进行。通常,字符串函数(strcpy
、strcat
、sprintf
等)用于修改字符串或对字符串进行赋值;示例:
//string literal used to initialize
char send[100] = "This text was sent by parent!"; //works
//initialized with nul terminator
char send[100] = '\0';
//post initialization statements:
strcpy(send, "This text was sent by parent!");//works
strcat(send, "This text was sent by parent!");//works
sprintf(send, "%s", "This text was sent by parent!");//works
但是:
此外,建议您始终检查malloc()
的返回,以确保确实分配了内存:
send = malloc(100);
if(!send) return -1; //check return of malloc
send = "This text was sent by parent!";
还有一点:因为receive
是一个指针,所以它的大小将是包含它的a所需的字节数