读取system()命令错误响应消息

时间:2018-05-15 11:57:41

标签: c++ compiler-errors system command-prompt

我正在尝试在Windows 10计算机上使用C ++ system()命令执行应用程序(Coq编译器)。这是我的代码:

string dospath = "coqc afile.v >> text.txt"; int errorno = system(dospath.c_str());

如果 afile.v 中存在语法/类型错误,则Coq会返回有意义的错误消息。目前,即使Coq返回错误,我也没有在text.txt中收到任何内容(我在C ++应用程序的命令提示符窗口中看到了错误消息)。我想在文件text.txt中读取(任意)消息(由 coqc 返回)作为字符串或文本。我知道有很多方法可以使用pstream.h来实现它,但是我无法让它们在我的Windows机器上运行。

2 个答案:

答案 0 :(得分:3)

使用>>仅捕获输出流。

使用2>>可以捕获错误流。

尝试执行:string dospath = "coqc afile.v >> text.txt 2>>error.txt"

答案 1 :(得分:1)

您可以将stderr重定向到stdout,如下所示:

string dospath = "coqc afile.v >> text.txt 2>&1";

但是有更好的方法来获取像CreateProcess

中的流