简体   繁体   中英

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?

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.

一种解决方案是将TIME参数作为参数传递,并使用./a.out time> output.txt将其输出到文件。

The simplest method is what sudo_O told you (it works in 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 goes to "myoutput.txt". Also redirects stdin to myinput.txt, so all scanfs are reading from "myinput.txt".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM