C++ 流重定向输出到调试

#include <Windows.h>
#include <ostream>
#include <sstream>
#include <string>
//using namespace std;

template < class CharT, class TraintT = std::char_traits<CharT> > 
class basic_debugbuf : public std::basic_stringbuf <CharT, TraintT>
{
    ~basic_debugbuf()
    {
        sync();
    }

protected:
    int sync()
    {
        WriteDebugString(str().c_str());
        str(std::basic_string<CharT>());    

        return 0;
    }

    void WriteDebugString(const CharT * msg)  {}
};

template <>
void basic_debugbuf<char>::WriteDebugString(const char *msg)
{
    ::OutputDebugStringA(msg);
}

template<>
void basic_debugbuf<wchar_t>::WriteDebugString(const wchar_t *msg)
{
    ::OutputDebugStringW(msg);
}

template< class CharT, class TraistT = std::char_traits< CharT> >
class basic_debugostream : public std::basic_ostream < CharT, TraistT>
{
public:
    basic_debugostream() : std::basic_ostream < CharT, TraistT> (new basic_debugbuf<CharT, TraistT> ()) {}
    ~basic_debugostream() { delete rdbuf(); }
};

typedef basic_debugostream<char> DebugStream;
typedef basic_debugostream<wchar_t> WDebugStream;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在调试助手上输入计算公式,并将结果输出到串口,你可以使用Arduino的Serial对象和eval()函数库。eval()函数库可以解析字符串中的数学表达式,并返回计算结果。你可以使用Serial.read()函数来读取调试助手上输入的字符串,并将其传递给eval()函数库进行计算。计算结果可以使用Serial.println()函数输出到串口。 下面是一个示例代码,演示了如何在调试助手上输入计算公式,并将结果输出到串口: ```C++ #include <Arduino.h> #include <SerialDebug.h> #include <Eval.h> void setup() { // 初始化串口 Serial.begin(9600); // 初始化调试助手 Debug.begin("SerialDebug"); // 设置eval()函数库的最大栈大小 eval::setStackSize(64); } void loop() { // 读取调试助手上输入的字符串 String input = Debug.read(); // 计算表达式并输出结果 if (input.length() > 0) { int result = eval::eval(input.c_str()); Serial.println(result); } } ``` 在这个例子中,我们使用了SerialDebug库和Eval库。SerialDebug库提供了一个简便的方法来读取调试助手上的输入,并且可以将输出直接发送到调试助手。Eval库提供了一个eval()函数,可以计算字符串中的数学表达式。 在setup()函数中,我们初始化了串口、调试助手和eval()函数库,并设置了eval()函数库的最大栈大小。 在loop()函数中,我们使用Debug.read()函数读取调试助手上的输入,并将其传递给eval()函数库进行计算。计算结果可以使用Serial.println()函数输出到串口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值