我正在使用c ++在unix中做一些工作.我试图在我的两个程序之间创建一个命名管道,并在它们之间来回发送一些文本.一切编译都很好,但当我调用我的系统运行server.cpp时,我收到此错误消息.
./server.cpp: line 8: syntax error near unexpected token '('
./server.cpp: line 8: 'void test()'
Run Code Online (Sandbox Code Playgroud)
导致此错误的原因是什么?我对unix或命名管道没有多少经验,所以我有点难过.
这是我的代码
client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int fd;
mkfifo("home/damil/myPipe", 0666);
fd=open("home/damil/myPipe", O_WRONLY);
write(fd,"test", sizeof("test")+1);
system("./server.cpp");
close(fd);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void test()
{
int fd;
char * comm;
fd = open("home/damil/myPipe", O_RDONLY);
read(fd, comm, 1024);
printf(comm);
close(fd);
}
Run Code Online (Sandbox Code Playgroud)