How to save the results. /a.out to a text file

I am wondering if there is a way to save the results of my program in a text file. I know that you can just do something like. /a.out> output.txt, but for my program, after type input. /a.out I am again offered TIME: where I insert the time, and then, when hit, enter the algorithm and the results are displayed.

The program displays the scene for a certain period of time, and basically my output is as follows:

time 0:00 stage 1 time 0:05 stage 1 ... time 2:05 stage 2 

How can I get the output stored in a text file?

+4
source share
4 answers

Also redirect input:

./a.out < input.txt > output.txt

Where input.txt contains the amount of time.

+5
source

One way to do this is to print the result before stderr

 fprintf(stderr, "time %d:...."); 

And redirect stderr to output.txt

 ./myprog 2> output.txt 

Note. This is a workaround, if you do not want to open the file, I do not like to use stderr for anything other than errors.

+2
source

One solution is to pass the TIME parameter as an argument and use it. /a.out time> output.txt to output it to a file.

+1
source

The easiest way is what sudo_O said (it works on every os)

 ./a.out <in.txt >out.txt. 

If you want to do this in C, use freopen () ( http://www.cplusplus.com/reference/clibrary/cstdio/freopen/ )

  freopen ("myoutput.txt","w",stdout); freopen ("myinput.txt","r",stdin); 

This redirects stdout to myoutput.txt, so all printfs are sent to "myoutput.txt". Also redirects stdin to myinput.txt so that all scanfs are read from "myinput.txt".

0
source

Source: https://habr.com/ru/post/1447882/


All Articles