如何在没有 shell 命令的情况下将终端输出镜像到文件?
我正在对一个项目进行收尾工作,但遇到了一些进退两难的情况。收集所有数据并计算统计数据后,结果将打印到屏幕上。但是,在程序中,用户可以选择将所有输出保存到文件中。我想以相同的格式将数据打印到终端和文件。
我考虑过执行 fork(),但这只是一个过程,并且数据输出是在程序终止之前完成的。如果我 fork,那么子进程将从头开始执行,并且成功实施将导致 500+ LOC 的不那么小的重写。
上学期我大致讨论了这个完全相同的主题,但我把我的 UNIX 编程书留在家里了,我发现没有一个例子适合我的需要。
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. 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑通过 tee 命令管道输出,该命令写入 stdout 和文件。
Consider piping your output through the tee command, which writes to stdout and a file.