我想检查cmd变量设置为“LISTALL”,但它不是当我尝试打印出来。C++字符数组不正确通过管道读取
#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
int main(int argc, char **argv)
{
pid_t cPid = fork();
int P2C[2];
int C2P[2];
pipe(P2C);
pipe(C2P);
char cmd[50];
char* listOfProcesses = new char[1024];
if (cPid == 0)
{
...
read(P2C[0], cmd, 50);
printf("%s\n", cmd);
if(strcmp(cmd,"LISTALL") == 0)
{
//printf("Executing the command: %s", cmd);
write(C2P[1], getlistOfProcesses("ps -ax -o pid,cmd"), 1024);
...
}
}
else if (cPid > 0)
{
...
write(P2C[1], "LISTALL", 50);
wait(NULL);
read(C2P[0], listOfProcesses,1024);
...
}
else
{
// fork failed
printf("Forking failed!\n");
exit(1);
}
return 0;
}
我从中得到的是一个迷你盒子符号,其顶部为00,底部为01或02。我尝试在这里粘贴符号,但没有显示。
如果'阅读(P2C [0],CMD,1050)''然后应该cmd'是大到足以容纳1050个字节。声明'char cmd [50]'不符合这个要求。 – Oswald
是啊,他们都设置为50现在,我已经忘了改变别人的时候我张贴,感谢尽管 – ILikeToLearn
“C++”和“字符数组” #doesnotcompute –