Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我用一些命令调用系统(char * 命令)并且它失败时,我想知道错误代码(而不是解析文本输出)。例如,我运行 system("rm file") 并且 'file' 不存在 - 如何将 ENOENT 接收到我的应用程序中?
您只能这样做是命令显式返回该状态。
rc = system(...); if (rc != -1 && WIFEXITED(rc)) printf("Terminated with status %d\n", WEXITSTATUS(rc));
返回值为 -1 错误(例如 fork(2)失败),否则返回命令状态。后一种返回状态采用 中指定的格式wait(2)。因此,该命令的退出代码将是WEXITSTATUS(status).
fork(2)
wait(2)
WEXITSTATUS(status)
但是,如果命令只是1在出现问题时返回,那么调用者就很难说出实际原因。
1