1

I am trying to execute an application (Coq compiler) using C++ system() command on my Windows 10 machine. Here is my code:

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

If there is a syntax/type error in afile.v, Coq returns a meaningful error message. Currently, I receive nothing in text.txt even if Coq returns an error (I see the error message in command prompt window of C++ application). I want to read that (any) message (returned by coqc) as a string or text in file text.txt. I know there are ways to do it using pstream.h but am not able to make them work on my Windows machine.

Khan
  • 303
  • 2
  • 14
  • Possible duplicate of [What does "2>&1" in a Windows Command Do?](https://stackoverflow.com/questions/42211476/what-does-21-in-a-windows-command-do) – Robert Andrzejuk May 15 '18 at 12:06
  • Another way how to output standard and error streams to the same file: https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file#1420981 – Robert Andrzejuk May 15 '18 at 12:20
  • If You want to capture the streams (in program), a lot more code is needed: https://ubuntuforums.org/showthread.php?t=610271&s=4976e90ae0bfdc6c2768cb47aa334e5a&p=3754153#post3754153 – Robert Andrzejuk May 15 '18 at 12:25

2 Answers2

3

With >> only the output stream is captured.

With 2>> the error stream can be captured.

Try to execute: string dospath = "coqc afile.v >> text.txt 2>>error.txt"

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
1

You can redirect stderr to stdout like this:

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

but there are better approach to get the streams like in CreateProcess

SHR
  • 7,940
  • 9
  • 38
  • 57