简体   繁体   English

如何将终端输出镜像到没有shell命令的文件?

[英]How to mirror terminal output to file without shell command?

I'm putting the finishing touches of a project, and have a bit of a dilemma. 我正在完成一个项目的最后润色,并且有点两难。 Once all the data is gathered and statistics calculated, the results are then printed to the screen. 收集完所有数据并计算统计数据后,结果将打印到屏幕上。 However, in the program, the user is given the option of saving all the output to a file. 但是,在程序中,用户可以选择将所有输出保存到文件中。 I'd like to print data to both the terminal and file with same formatting. 我想以相同的格式将数据打印到终端和文件。

I considered doing a fork(), but this is all one process and the data output is done just before the program termination. 我考虑做一个fork(),但这是一个进程,数据输出在程序终止之前完成。 If I fork, then the child process will start executing from the beginning, and implementing successfully would result in a not so minor rewrite of 500+ LOC. 如果我fork,那么子进程将从头开始执行,并且成功实现将导致500+ LOC的重写不那么重要。

I covered roughly this exact same topic last semester, but left my unix programming book at home and none of the examples I've found fit my needs. 我在上学期大致涵盖了这个完全相同的主题,但在家里留下了我的unix编程书,没有一个我认为符合我需要的例子。

考虑通过tee命令管道输出,该命令写入stdout和文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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