C++ 如何写入 Visual Studio 中的输出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1149620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write to the Output window in Visual Studio?
提问by clamp
Which function should I use to output text to the "Output" window in Visual Studio?
我应该使用哪个函数将文本输出到 Visual Studio 中的“输出”窗口?
I tried printf()
but it doesn't show up.
我试过了,printf()
但它不显示。
回答by Sorantis
OutputDebugStringfunction will do it.
OutputDebugString函数会做到这一点。
example code
示例代码
void CClass::Output(const char* szFormat, ...)
{
char szBuff[1024];
va_list arg;
va_start(arg, szFormat);
_vsnprintf(szBuff, sizeof(szBuff), szFormat, arg);
va_end(arg);
OutputDebugString(szBuff);
}
回答by clamp
If this is for debug output then OutputDebugStringis what you want. A useful macro :
如果这是用于调试输出,那么OutputDebugString就是您想要的。一个有用的宏:
#define DBOUT( s ) \
{ \
std::ostringstream os_; \
os_ << s; \
OutputDebugString( os_.str().c_str() ); \
}
This allows you to say things like:
这允许你说这样的话:
DBOUT( "The value of x is " << x );
You can extend this using the __LINE__
and __FILE__
macros to give even more information.
您可以使用__LINE__
和__FILE__
宏对其进行扩展以提供更多信息。
For those in Windows and wide character land:
对于 Windows 和宽字符域中的用户:
#include <Windows.h>
#include <iostream>
#include <sstream>
#define DBOUT( s ) \
{ \
std::wostringstream os_; \
os_ << s; \
OutputDebugStringW( os_.str().c_str() ); \
}
回答by Reunanen
Use the OutputDebugString
function or the TRACE
macro (MFC) which lets you do printf
-style formatting:
使用OutputDebugString
函数或TRACE
宏 (MFC) 可以让您进行printf
样式格式化:
int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement\n" );
TRACE( "The value of x is %d\n", x );
TRACE( "x = %d and y = %d\n", x, y );
TRACE( "x = %d and y = %x and z = %f\n", x, y, z );
回答by parsley72
Useful tip - if you use __FILE__
and __LINE__
then format your debug as:
有用的技巧-如果你使用__FILE__
和__LINE__
再格式化调试为:
"file(line): Your output here"
then when you click on that line in the output window Visual Studio will jump directly to that line of code. An example:
然后当您在输出窗口中单击该行时,Visual Studio 将直接跳转到该行代码。一个例子:
#include <Windows.h>
#include <iostream>
#include <sstream>
void DBOut(const char *file, const int line, const WCHAR *s)
{
std::wostringstream os_;
os_ << file << "(" << line << "): ";
os_ << s;
OutputDebugStringW(os_.str().c_str());
}
#define DBOUT(s) DBOut(__FILE__, __LINE__, s)
I wrote a blog post about this so I always knew where I could look it up: https://windowscecleaner.blogspot.co.nz/2013/04/debug-output-tricks-for-visual-studio.html
我写了一篇关于这个的博客文章,所以我总是知道在哪里可以找到它:https: //windowscecleaner.blogspot.co.nz/2013/04/debug-output-tricks-for-visual-studio.html
回答by Andrew
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
wstring outputMe = L"can" + L" concatenate\n";
OutputDebugString(outputMe.c_str());
回答by Maurizio TALPO
Use OutputDebugString instead of afxDump.
使用 OutputDebugString 而不是 afxDump。
Example:
例子:
#define _TRACE_MAXLEN 500
#if _MSC_VER >= 1900
#define _PRINT_DEBUG_STRING(text) OutputDebugString(text)
#else // _MSC_VER >= 1900
#define _PRINT_DEBUG_STRING(text) afxDump << text
#endif // _MSC_VER >= 1900
void MyTrace(LPCTSTR sFormat, ...)
{
TCHAR text[_TRACE_MAXLEN + 1];
memset(text, 0, _TRACE_MAXLEN + 1);
va_list args;
va_start(args, sFormat);
int n = _vsntprintf(text, _TRACE_MAXLEN, sFormat, args);
va_end(args);
_PRINT_DEBUG_STRING(text);
if(n <= 0)
_PRINT_DEBUG_STRING(_T("[...]"));
}
回答by amn
Even though OutputDebugString
indeed prints a string of characters to the debugger console, it's not exactly like printf
with regard to the latter being able to format arguments using the %
notation and a variable number of arguments, something OutputDebugString
does not do.
尽管OutputDebugString
确实将一串字符打印到调试器控制台,但printf
与后者能够使用%
符号和可变数量的参数格式化参数的情况并不完全一样,但有些事情OutputDebugString
并没有做到。
I would make the case that the _RPTFN
macro, with _CRT_WARN
argument at least, is a better suitor in this case -- it formats the principal string much like printf
, writing the result to debugger console.
在这种情况下,我会证明_RPTFN
宏_CRT_WARN
(至少带有参数)是更好的选择器——它格式化主字符串很像printf
,将结果写入调试器控制台。
A minor (and strange, in my opinion) caveat with it is that it requires at least one argumentfollowing the format string (the one with all the %
for substitution), a limitation printf
does notsuffer from.
A小调(和奇怪,在我看来)警告与它是,它需要至少一个参数,格式字符串(一个与下面所有%
的替代),限制printf
并没有患。
For cases where you need a puts
like functionality -- no formatting, just writing the string as-is -- there is its sibling _RPTF0
(which ignores arguments following the format string, another strange caveat). Or OutputDebugString
of course.
对于需要puts
类似功能的情况——没有格式化,只是按原样编写字符串——有它的兄弟_RPTF0
(忽略格式字符串后面的参数,另一个奇怪的警告)。或者OutputDebugString
当然。
And by the way, there is also everything from _RPT1
to _RPT5
but I haven't tried them. Honestly, I don't understand why provide so many procedures all doing essentially the same thing.
顺便说一句,还有从_RPT1
到的所有内容,_RPT5
但我还没有尝试过。老实说,我不明白为什么要提供这么多基本上都做相同事情的程序。