C++ Visual Studio - 如何在非控制台应用程序中查看 cout 输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16703835/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:33:40  来源:igfitidea点击:

Visual Studio - How can I see cout output in a non-console application?

c++visual-studio

提问by CDT

It seems rather tedious to output to debug window. Where can I find coutoutput if I am writing a non-console information ?

输出到调试窗口似乎相当乏味。cout如果我正在编写非控制台信息,我在哪里可以找到输出?

Like:

喜欢:

double i = a / b;
cout << b << endl;//I want to check out whether b is zero. It seems the output cannot be found anywhere.

采纳答案by Thomas Matthews

To output a string to the debug console, use OutputDebugStringA. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx

要将字符串输出到调试控制台,请使用OutputDebugStringA. 请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx

To output variable values to the debug console, using std::ostringstream, the send the string to OutputDebugStringA.

要将变量值输出到调试控制台,请使用std::ostringstream,将字符串发送到OutputDebugStringA

Excessive output statements will cause the program to severly slow down. However, it is a good technique to catch things the debugger has a problem with, such as the actual child members when playing with base pointers.

过多的输出语句会导致程序严重变慢。然而,这是一种很好的技术来捕捉调试器有问题的东西,例如在使用基指针时实际的子成员。

回答by Marius Amado-Alves

The question is very clear. How use std::cout to debug a non-console application in Visual Studio.

这个问题很清楚。如何使用 std::cout 在 Visual Studio 中调试非控制台应用程序。

The answer is very clear: you cannot. That is, Visual Studio does not support std::cout as debug tool for non-console applications.

答案很明确:你不能。也就是说,Visual Studio 不支持 std::cout 作为非控制台应用程序的调试工具。

This is a serious limitation of Visual Studio, probably a failure to meet the C++ standard even. I find it very sad to see disinformative "answers" here trying to hide this defect of their precious Visual Studio.

这是 Visual Studio 的严重限制,甚至可能无法满足 C++ 标准。我发现在这里看到虚假的“答案”试图隐藏他们宝贵的 Visual Studio 的这个缺陷感到非常难过。

回答by Trevor Hickey

For a Windows solution, you can allocate a console, and bind cout/cin to it. For example:

对于 Windows 解决方案,您可以分配一个控制台,并将 cout/cin 绑定到它。例如:

AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);  

Documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681944%28v=vs.85%29.aspx

文档:https:
//msdn.microsoft.com/en-us/library/windows/desktop/ms681944%28v=vs.85%29.aspx

回答by Luc Bloom

SOLUTION: This answer solves the question and allows you to redirect console output to the Visual Studio Output window. First we need a class that overrides the default cout string stream:

解决方案:此答案解决了问题,并允许您将控制台输出重定向到 Visual Studio 输出窗口。首先,我们需要一个覆盖默认 cout 字符串流的类:

class dbg_stream_for_cout
    : public std::stringbuf
{
public:
    ~dbg_stream_for_cout() { sync(); }
    int sync()
    {
        ::OutputDebugStringA(str().c_str());
        str(std::string()); // Clear the string buffer
        return 0;
    }
};
dbg_stream_for_cout g_DebugStreamFor_cout;

Then, somewhere you want to "activate" writing to the VS output window:

然后,您想在某个地方“激活”写入 VS 输出窗口:

std::cout.rdbuf(&g_DebugStreamFor_cout); // Redirect std::cout to OutputDebugString!

回答by Stefano Buora

I'd like to give my 2 cents.

我想给我的 2 美分。

Given that the maybe is a VS issue about compliancy with the C++ standard or that we could use OutputDebugStringA, if you cannot modify your code base you may like the idea of simply redirect the std::cout to something else, like a file.

鉴于这可能是关于符合 C++ 标准的 VS 问题,或者我们可以使用OutputDebugStringA,如果您无法修改您的代码库,您可能会喜欢简单地将 std::cout 重定向到其他内容(例如文件)的想法。

So without changing your code base you can do something like suggested here:How to redirect cin and cout to files?

因此,在不更改代码库的情况下,您可以执行以下建议的操作:如何将 cin 和 cout 重定向到文件?

Condensed:

浓缩:

  • add the include #include <fstream>
  • at the beginning of your app, in some init, before logging you can use:
  • 添加包含 #include <fstream>
  • 在您的应用程序开始时,在某些 init 中,在登录之前,您可以使用:
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
  • the at the end of your app/logging:
  • 在您的应用程序/日志记录结束时:

std::cout.rdbuf(coutbuf); //reset to standard output again

std::cout.rdbuf(coutbuf); //再次重置为标准输出

Hope this may help someone, Kudos to Nawaz that provided the answer in the other thread.

希望这可以帮助某人,感谢 Nawaz 在另一个线程中提供了答案。

回答by AliBZ

Instead of using cout, create a log file and write anything you want into it.

不要使用 cout,而是创建一个日志文件并将您想要的任何内容写入其中。

Edit: Use this simple code for writing to a log file.

编辑:使用这个简单的代码写入日志文件。

ofstream log;
log.open ("log.txt");
log << "Writing this to a file.\n";
log.close();

回答by Mohamed Ahmed Abdelraheem

You can use .Net functions such as System::Diagnostics::Debug::WriteLine("your message"). You can even add a condition to print only during the debug mode and not in the release mode. For example:

您可以使用 .Net 函数,例如 System::Diagnostics::Debug::WriteLine("your message")。您甚至可以添加一个仅在调试模式下而不是在发布模式下打印的条件。例如:

#ifdef DEBUG   
   System::Diagnostics::Debug::WriteLine("your message");
#endif