[英]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.
我知道你可以做类似./a.out> output.txt的事情,但对于我的程序,在我键入./a.out之后我再次被提示为TIME:我把时间放入,然后点击输入算法并输出结果。
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. 其中
input.txt
包含时间量。
One way to do it is to print the result to stderr
一种方法是将结果打印到
stderr
fprintf(stderr, "time %d:....");
And redirect stderr
to output.txt
并将
stderr
重定向到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. 注意:如果您不想打开文件,这是一种解决方法,我不喜欢将
stderr
用于除错误之外的任何其他操作。
一种解决方案是将TIME参数作为参数传递,并使用./a.out time> output.txt将其输出到文件。
The simplest method is what sudo_O told you (it works in every os) 最简单的方法是sudo_O告诉你的(它适用于每个操作系统)
./a.out <in.txt >out.txt.
If you want to do this in C, use freopen() ( http://www.cplusplus.com/reference/clibrary/cstdio/freopen/ ) 如果要在C中执行此操作,请使用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". 这会将stdout重定向到myoutput.txt,因此所有printfs都会转到“myoutput.txt”。 Also redirects stdin to myinput.txt, so all scanfs are reading from "myinput.txt".
还将stdin重定向到myinput.txt,因此所有scanfs都从“myinput.txt”读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.