0

I have an Executable program which runs the other program named main.cpp from it.

Main.cpp both have stdout and stderr message

I need to redirect both stdout and stderr message to one file .

I tried with different option but none of them helps.

This is executable.cpp

I got all the output in terminal when I tried this, but empty file some.txt created.

system("g++ -g main.cpp -o a.out");
system("./a.out &>> some.txt");

Only the stderr message is saved in some.txt but stdout messagae is dispalyed on terminal.

system("./a.out 2>> some.txt");

How can I save both message to other file at a single time while running program.

Jon marsh
  • 279
  • 7
  • 12
  • Try `./a.out 2>>&1 >>some.txt` or `./a.out >> some.txt 2>>&1`. Just one of them will work, but im not sure which right now... Or maybe there should be just `2>&1`, im not really used to use the append output redirection... – slepic Sep 06 '19 at 07:20
  • try this : system("./a.out >> some.txt 2>&1"); – ChauhanTs Sep 06 '19 at 07:25
  • @slepic redirection operators are processed from left to right. So first reopen stdout to append (`>> some.txt`) and then reopen stderr to stdout: (`2>&1`) – Botje Sep 06 '19 at 07:53
  • @Botje yeah, sure, I justed wanted to give a quick answer and wasn't sure about the right combination, as I dont do this a lot, but I was sure one of the combinations is right :) I would figure it out after a bit of thinking or trying it out, but I supposed the OP can try for himself... – slepic Sep 06 '19 at 08:06
  • Thank you all,```system("./a.out >> some.txt 2>&1");``` this works partially, but the problem is output statements in main.cpp program is not getting appended on that file. All the error messages can be seen in the file but executed statement can not be seen. – Jon marsh Sep 06 '19 at 09:07

0 Answers0