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?
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.
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.
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.
So redirect the input as well:
./a.out < input.txt > output.txt
Where input.txt
contains the amount of time.
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.
One solution is to pass the TIME parameter as an argument and use ./a.out time > output.txt to output it to a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With