if 语句 - C++ 不访问 if语句 中的方法
if statement - C++ not accessing method in ifstatement
if 语句块现在在我在此处添加 时执行:
printf("Executing the command: %sn", cmd);
在此添加之前,它没有命中getListofProcesses()方法。原始代码如下所示:
#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
char* getlistOfProcesses(const char* cmd)
{
FILE* pipe = popen(cmd, "r");
if (!pipe) return (char*)"ERROR";
char buffer[128];
char *result = new char[1024];
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
strcat(result, buffer);
}
pclose(pipe);
return result;
}
int main(int argc, char **argv)
{
int P2C[2];
int C2P[2];
pipe(P2C);
pipe(C2P);
pid_t cPid = fork();
char cmd[50];
char* listOfProcesses = new char[1024];
if (cPid == 0)
{
close(P2C[1]);
close(C2P[0]);
read(P2C[0], cmd, 50);
if(strcmp(cmd,"LISTALL") == 0)
{
printf("Executing the command: %s", cmd);
write(C2P[1], getlistOfProcesses("ps -ax -o pid,cmd"), 1024);
close(P2C[0]);
close(C2P[1]);
}
}
else if (cPid > 0)
{
close(C2P[1]);
close(P2C[0]);
write(P2C[1], "LISTALL", 50);
wait(NULL);
read(C2P[0], listOfProcesses,1024);
printf("%sn",listOfProcesses);
close(C2P[0]);
close(P2C[1]);
}
...
return 0;
}
我不确定这是由于我的管道完成方式还是正在访问它并且我没有注意到,我试图在方法顶部附近使用 printf 进行检查。
您需要
在格式字符串中包含换行符,或在 printf 语句后调用 fflush(stdout)
。请参阅为什么 printf 在调用后不刷新,除非格式字符串中有换行符?了解更多信息。
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复