Visual Studio C++ Empty Project cout?

Visual Studio C++ Empty Project cout?

本文关键字:cout Project Empty Studio C++ Visual      更新时间:2023-10-16

我是一个Java人(我知道相当多的C),他正试图进入C++。

目前我正在使用VisualStudio 2012 Express,并创建了一个空项目。

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

真的再简单不过了。然而,我无法让该死的输出出现在我的生命中。

'Spark.exe' (Win32): Loaded 'C:UsersSmith_000DocumentsVisual Studio 2012ProjectsSparkDebugSpark.exe'. Symbols loaded.
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64ntdll.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64kernel32.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64KernelBase.dll'. Cannot find or open the PDB file.
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64apphelp.dll'. Cannot find or open the PDB file.
SHIMVIEW: ShimInfo(Complete)
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcp110d.dll'. Symbols loaded.
'Spark.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcr110d.dll'. Symbols loaded.
The program '[6260] Spark.exe' has exited with code 0 (0x0).

在阅读本文之后,似乎创建一个空项目会禁用我在大学时所做的简单 C 项目中习惯的控制台。

那么,当调试

模式处于活动状态时,有哪些致命的简单方法可以将基本的调试文本(如printf,cout和cerr)放入VS(首选)或控制台中?

谢谢!

通过使用 OutputDebugStringA 的自定义缓冲区路由cout/cerr/clog

#include <iostream>
#include <windows.h>
using namespace std;
// routes cout/cerr/clog to VC++ console and good old c stdout
class custom_outputbuffer : public std::streambuf {
public:
    custom_outputbuffer() 
    {
        setp(0, 0);
        // set std::cout to use my custom streambuf
        std::streambuf *backupOut = std::cout.rdbuf(this);
        std::streambuf *backupErr = std::cerr.rdbuf(this);
        std::streambuf *backupLog = std::clog.rdbuf(this);
    }
    ~custom_outputbuffer()
    {
        // make sure to restore the original so we don't get a crash on close!
        std::cout.rdbuf(backupOut);
        std::cerr.rdbuf(backupErr);
        std::clog.rdbuf(backupLog);
    }
    virtual int_type overflow(int_type c = traits_type::eof())
    {
        currentLine.push_back(c);
        int value = fputc(c, stdout);
        if (value == EOF || value == 'n')
        {
            OutputDebugStringA(currentLine.c_str());
            currentLine.clear();
        }
        return c;
    }
    std::string currentLine;
    std::streambuf *backupOut;
    std::streambuf *backupErr;
    std::streambuf *backupLog;
};

int main(int argc, char * argv[])
{   
    custom_outputbuffer ob;
    cout << "Hello Visual Studio Debug Output" << endl;
    return 0;
}

这将打开一个带有文本的控制台窗口并快速关闭它。 可以使用 OutputDebugString 将信息发送到 Visual Studio 中的"输出"窗口。例如

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    OutputDebugString("Hello Worldn");
    return 0;
}

致命的简单方法:

将光标放在行上:

return 0;

F9 设置断点。您应该看到左侧出现一个大红点。这将允许您在程序从 main 返回之前查看控制台输出。读取完控制台输出后,按 F5 继续执行。

若要查看 Windows IDE 输出中的文本,请使用 OutputDebugString()

见 http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362%28v=vs.85%29.aspx

否则,它会转到控制台(如果您需要控制台,请参阅您的设置,但要知道它会在程序退出后立即关闭,以防止此类使用getchar()暂停,直到您按 Enter 键。

更好的是,在我看来,切换到Linux...