【发布时间】:2021-10-13 09:36:07
【问题描述】:
我有一个使用 Linux 终端运行 python 程序的 C 程序,但我想确保 python 程序运行时没有错误。如果python程序运行后终端打印错误信息,我希望C程序知道。
运行.py:
g = input(">")
if (g == 0):
print("error")
exit(1)
else:
print("good")
exit(0)
checker.c:
#include <stdio.h>
#include <stdlib.h>
int main(void){
char run[30];
snprintf(run, 30, "sudo python2.7 run.py");
// I need to make sure that the next statement is run without errors
system(run);
【问题讨论】:
-
这可能有与 C 检查命令“sudo mv nonexistant.x”中的错误类似的解决方案,但我也不知道该怎么做
-
试试popen。或者只是检查
system()的返回码。任何非零都表示错误。 -
使用
popen是一种选择,但您可以编写一个C 程序在管道中使用。管道程序在标准输入上接收程序的输出。所以你可以在命令行中使用:pyprog | Cprog,通过这样做,C 程序将在标准输入上接收到 python 程序的所有输出。 -
那里给出的代码在 C 中的工作方式相同。
-
什么指示错误消息?在流
stderr上输出?有输出吗?以error、Error、ERROR或令牌Failure开头的消息?还是只是子进程的返回值?在后一种情况下,man system 解释了调用的返回值。
标签: python c linux error-handling