bash脚本在使用popen()时不返回代码以调用C ++程序
bash script not returning return code to calling c++ program when using popen()
我有一个python脚本,它在bash脚本中被调用,如下所示:
python3 CheckRef.py --reference $EXPECTED --result $RESULT.npy
echo "return code = $?"
该脚本从 python 脚本打印正确的返回代码。例如,如果 python 脚本返回-2
,它会打印254
。
但是,当我在 c++ 程序中调用 bash 脚本时,返回代码为空。它应该返回-2
.
testClass test;
auto result = test.execute(command.c_str());
std::cout << "result is " << result << std::endl;
谁能告诉我做错了什么?
这是函数execute()
std::string testClass::execute(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
您的execute
函数返回一个字符串,其中包含命令的输出,而不是终止代码。
进程的终止状态代码由pclose
返回。当前,您放弃对pclose
调用的返回值
相关文章:
- 为什么这段代码返回 -nan(ind)?C++
- 为什么我的代码返回了错误的平方值?C++
- 从本机代码返回到托管代码会损坏返回的对象
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- 为什么此代码返回分段错误错误?
- C 语法,根据此代码返回值类型
- 为什么在下面的代码返回类型中是用于运营商重载的类类型
- 为什么下面的使用链表实现线性队列的代码返回垃圾值然后崩溃
- 为什么下面的代码返回向量大小为 10
- 从本机代码返回"const char*"并在java中获取"字符串"
- JNI 代码返回字符串,但得到错误 java.lang.UnsatisfiedLinkError: com.packag
- 相同的数字代码返回不同的输出,无论是 C++ 还是 C
- C/C++ 从 x86 代码返回
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 代码块 10.05 和 12.11、13.12 使用相同的代码返回不同的结果
- C++将字符串转换为 wstring 并使用最小代码返回
- 混合托管C++方法并不总是向调用C#代码返回相同的结果
- 可以将用于向std::字符串返回const ref的函数更改为通过值break调用代码返回
- 请解释这段代码(返回语句)
- 项目欧拉任务#8在c++中,代码返回错误答案的微妙原因