简体   繁体   English

如何在Visual Studio C ++中在控制台中记录内容

[英]How to log stuff in console in Visual Studio C++

I'm working on a little C++-Game in Visual Studio 2008. I want to see the content of a vector after a couple of seconds or after I pressed some buttons. 我正在研究一个小小的C ++ - Visual Studio 2008中的游戏。我希望在几秒钟后或按下一些按钮后看到矢量的内容。 Breakpoints are useless in this case, because they stop me at every call of the gameloop (~60 times per second). 在这种情况下,断点是无用的,因为它们在每次调用游戏循环时都会阻止我(每秒约60次)。 How do I debug in this case? 在这种情况下我该如何调试?

Thanks! 谢谢!

Use function OutputDebugString from Windows API. 使用Windows API中的OutputDebugString函数。 You can call it anytime you want eg every 100th loop in your code. 您可以随时调用它,例如代码中的每个第100个循环。

Function info is here 功能信息在这里

Please read all comments on this page - some people claim that in your IDE (VS2008) output of this function is shown in "Immediate Window" not the "Output". 请阅读本页面上的所有评论 - 有些人声称在你的IDE(VS2008)中,此功能的输出显示在“立即窗口”而非“输出”中。

You can set conditional breakpoints, ie breakpoints which hit at a certain position only when a given expression is true. 您可以设置条件断点,即仅当给定表达式为真时才在某个位置命中的断点。 You can, for example, set a breakpoint which hits only every nth time in a loop. 例如,您可以设置一个断点,该断点仅在循环中每第n次命中一次。

you can use a simple output to the console. 您可以使用简单的输出到控制台。

say you want to display an integer, you can simply use printf for example: 假设你想显示一个整数,你可以简单地使用printf例如:

printf("the number is %d \n", vectorArray.at(place) );

Setup an elapsed timer and something extremely basic. 设置一个经过时间的计时器和非常基本的东西

if elapsedTime > 3 seconds: hits your break point, check out your vector 如果elapsedTime> 3秒:点击你的断点,看看你的向量

Or if you want to stop on a very specific point, just flag a counter to keep track of how many frames you've done. 或者,如果您想要停留在一个非常具体的点上,只需标记一个计数器以跟踪您已完成的帧数。

Along with conditional breakpoints you can also have the breakpoint write the vector values to the console and not stop. 除了条件断点,您还可以让断点将向量值写入控制台而不是停止。

Right click on your breakpoint and select "When Hit" click "print a message" and then add your values to the message in curly braces. 右键单击断点并选择“When Hit”时单击“打印消息”,然后将值添加到花括号中的消息中。 Use the "Hit Count" to have the breakpoint execute after so many cycles. 使用“命中计数”使断点在这么多周期后执行。 The "Condition" option is also useful for setting the breakpoint dependent on a certain value in your variables. “条件”选项对于根据变量中的某个值设置断点也很有用。

You can also set your breakpoint inside a piece of conditional code, eg: 您还可以在一段条件代码中设置断点,例如:

if(keyPressed('S'))
{
  int a = 42; // <-- set breakpoint here
}

The pro vs a conditional breakpoint is that the condition can be a bit more complex, the con is that each time you need to change the condition, you need to compile and link your app. pro与条件断点的关系是条件可能有点复杂, con是每次需要更改条件时,需要编译并链接应用程序。

i found out that if you include and use fprintf(stdout,"") 我发现如果你包括并使用fprintf(stdout,“”)

it returns a command prompt on the screen while your program is still running 当程序仍在运行时,它会在屏幕上返回一个命令提示符

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

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