简体   繁体   English

使用cmd.exe显示或捕获完整的程序输出

[英]Show or capture complete program output using cmd.exe

I'm practising writing recursive functions using Visual Studio 2015 on Windows 7. 我正在练习在Windows 7上使用Visual Studio 2015编写递归函数。

I use cout to track the progress of my code, but it shows too many results and even though I stop the program I can not see the initial result... I can only see the output from the middle. 我使用cout跟踪我的代码进度,但是它显示了太多结果,即使我停止了程序,我也看不到初始结果……我只能看到中间的输出。

How can I see the complete program output? 如何查看完整的程序输出?

The problem is that cmd.exe (the Windows commad prompt) has a fixed-size buffer for displaying output. 问题在于cmd.exe (Windows命令行提示符)具有用于显示输出的固定大小的缓冲区。 If your program writes a lot of output, only the last N lines will be displayed, where N is the buffer size. 如果程序写入大量输出,则仅显示最后N行,其中N是缓冲区大小。

You can avoid this problem in several ways: 您可以通过几种方式避免此问题:

  1. Write to a file, instead of to std::cout . 写入文件,而不是std::cout All of your output will be captured in the file, which you can read in the text editor of your choice. 您的所有输出都将捕获到文件中,您可以在所选的文本编辑器中读取该文件。

  2. Redirect the standard output to a file. 将标准输出重定向到文件。 Run your program as my_prog.exe > output.log and the output will be redirected to output.log . my_prog.exe > output.log运行程序,输出将重定向到output.log

  3. Pipe your output to the more command, to show it one screen at a time: my_prog.exe | more 将输出输出到more命令,一次显示一个屏幕: my_prog.exe | more my_prog.exe | more

  4. Increase the cmd.exe buffer size. 增加cmd.exe缓冲区大小。 If you right-click on the title bar of the command window, you can select the Properties menu option. 如果右键单击命令窗口的标题栏,则可以选择“ 属性”菜单选项。 Within the Layout tab, you'll see a section called Screen buffer size . 在“ 布局”标签中,您会看到名为屏幕缓冲区大小的部分。 Change the Height to a larger value, and you'll be able to capture that many lines of output. 将“ 高度”更改为更大的值,您将能够捕获那么多行输出。 Note that this is somewhat unreliable, since you often don't know in advance of executing your program how many lines it will output. 请注意,这在某种程度上是不可靠的,因为在执行程序之前您通常不知道它将输出多少行。 One of the other approaches, using files, is often a better solution. 使用文件的其他方法之一通常是更好的解决方案。

Note that this isn't really a problem with your C++ program. 请注意,这并不是您的C ++程序真正的问题。 It's entirely reasonable to be able to produce a large quantity of output on the standard output stream. 能够在标准输出流上产生大量输出是完全合理的。 The best solutions are the ones that redirect or pipe the output to a file. 最好的解决方案是将输出重定向或通过管道传输到文件的解决方案。 These operations are available on most sensible platforms (and Windows, too) and do exactly what you need without having to change your program to write to a file. 这些操作在大多数明智的平台(以及Windows)上都可用,并且可以完全执行您所需的操作,而无需更改程序以写入文件。

I'm not sure to understand your problem, maybe you should write the output in a file instead of the standard output ? 我不确定要了解您的问题,也许您应该将输出写入文件而不是标准输出中? Then you will see all your results 然后,您将看到所有结果

从命令行运行应用程序,然后将输出重定向到文件:

yourapp.exe > yourapp.log

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

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