Visual Studio C++ Empty Project cout?
Visual Studio C++ Empty Project cout?
我是一个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...
相关文章:
- Project Euler问题4的错误解决方案
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- Codelite C++ new project
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- Visual Studio - Cmake Project - Add NetCDF
- 在 COUT 语句中使用 COUT 调用函数
- GCC 4.8.2 自动矢量化由于 cout 而失败
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- C++ Setter/Getter,cout 工作,printf 失败
- C++ cout 将双精度对齐到精度 2 并正确对齐
- Visual Studio C++ Project File CustomBuild Task: Filter Outp
- 结构元素名称要 cout?
- 使用模板类重载 cout
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- Visual Studio C++ Empty Project cout?