【问题标题】:ofstream writes hex instead of char*ofstream 写入 hex 而不是 char*
【发布时间】:2015-07-25 20:55:36
【问题描述】:

我尝试编写一个简单的 Logger,但 ofstream 将十六进制而不是字符写入文件。 定义:

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#define LOG Logger::getInstance()
class Logger
{
public:
    static Logger m_instance;
    static Logger &getInstance()
    {
        if (m_logfile == nullptr)
        {
            m_logfile = new std::ofstream(m_logfile_string.c_str(),
                                          std::ofstream::out | std::ofstream::app);
        }
        return m_instance;
    }

    Logger &operator<<(const std::string &c);
    Logger &operator<<(const char c[]);

private:
    static std::ofstream *m_logfile;
    static std::string m_logfile_string;
    Logger() {};
    Logger(const Logger &) = delete;
    Logger(Logger &&other) = delete;
    Logger &operator=(const Logger &other) = delete;
    Logger &operator=(Logger &&other) = delete;

    ~Logger()
    {
        if (m_logfile != nullptr)
            m_logfile->close();
    };

    std::string currentDateTime() const;
};

实施。

#include "Logger.h"
#include <iostream>
#include <ctime>

Logger Logger::m_instance;
std::ofstream *Logger::m_logfile = nullptr;
std::string Logger::m_logfile_string = "log.txt";

Logger &Logger::operator<<(const std::string &c)
{
    this->operator<<(c.c_str());
    return *this;
}

Logger &Logger::operator<<(const char c[])
{
    std::cout << currentDateTime() << " - "
              << c << std::endl;
    m_logfile->operator<<(c);
    return *this;
}

// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
std::string Logger::currentDateTime() const
{
    auto now = time(nullptr);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}

用法:

#include "Logger.h"
void main()
{
    LOG << "started"; // Logger::getInstance() << "started";
}

结果: 00007FF696EF3C00中的log.txt控制台输出是对的。

这里出了什么问题?

【问题讨论】:

  • 你尝试Logger &amp;Logger::operator&lt;&lt;(const char* c)了吗?
  • 如果我将方法更改为const char*,结果相同
  • 它打印的是指针而不是字符串的内容。您使用的是哪个工具链和操作系统?使用 clang 和 gcc 在 Mac OS X 上似乎可以正常工作。
  • 在 Windows 8.1 上使用 VS13 v120
  • 发布一个完整的最小测试用例,我们可以将其转储到编译器中。这些“...”都不是废话,您也没有准确说明输入和输出。

标签: c++ file ofstream


【解决方案1】:

你使用iostream的成员函数:

m_logfile->operator<<(c);

没有成员函数operator&lt;&lt;(char* c),但是有operator&lt;&lt;(void* p),所以指针被隐式转换。请参阅ostream 上的文档。

char* 的运算符不是类成员函数:

 ostream& operator<< (ostream& os, const char* s);

请看这里:operator<< (ostream)

所以你需要这个代码:

operator<<(*m_logfile, c);

或者更干净:

(*m_logfile) << c;

测试示例:

#include <iostream>

int main() {
    std::cout.operator<<("test");
    operator<<(std::cout, "test");
    std::cout << "test";
}

打印0x400944testtest

【讨论】:

  • 好的,这肯定会清除它。没有考虑到这里发生的隐含约定。感谢您的澄清。
【解决方案2】:

如果你使用

(*m_logfile)<<c;

而不是

m_logfile->operator<<(c);

它会起作用的。

原因:

  • 您的语法调用成员函数ostream::operator&lt;&lt;(),它既没有为char* 定义,也没有为string 定义。它只为void* 定义,它总是以十六进制显示地址。
  • 这里的经典语法调用非成员函数 ostream std::operator&lt;&lt;(...),它具有 stringchar* 的重载

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多