Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store results of ./a.out to a text file

I am wondering if there is any way to store the results of my program into a text file. I know you can just do something like ./a.out > output.txt but for my program, after I type ./a.out I am prompted again for TIME: where I put in the amount of time, and then upon hitting enter the algorithm is performed and the results are output.

The program outputs a stage for a period of time, and basically my output looks like this:

time 0:00 stage 1

time 0:05 stage 1

...

time 2:05 stage 2

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

like image 378
Chea Indian Avatar asked Nov 23 '12 08:11

Chea Indian


People also ask

How do I save output as text in python?

To write to a text file in Python, you follow these steps: First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.

Is a out a text file?

out files are text files and because of that, they can be opened with the use of any text editor, making it easy for these files to be accessed in case they are required.

How do you write the output of a command to a file?

Redirect Output to a File Only To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


3 Answers

So redirect the input as well:

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

Where input.txt contains the amount of time.

like image 134
Chris Seymour Avatar answered Nov 06 '22 11:11

Chris Seymour


One way to do it is to print the result to stderr

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

And redirect stderr to output.txt

./myprog 2> output.txt

Note: this is a workaround if you don't wish to open a file, I don't like using stderr for anything other than errors.

like image 24
iabdalkader Avatar answered Nov 06 '22 11:11

iabdalkader


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

like image 1
George Avatar answered Nov 06 '22 13:11

George