C++ fstream 只保存字符串的第一个字符

C++ fstream is only saving the first character of a string

正如标题所说,我对 fstream 有疑问。我制作了一个单例日志记录 class,它使用 fstream 将日志消息保存在一个文件中。

我用的是IDEXCode。在 Visual Studio 2013 年,代码运行良好。

Header 文件:

#ifndef Logger_hpp
#define Logger_hpp

#include <stdio.h>
#include <fstream>
#include <string>

class Logger
{
private:
    static Logger* loggerPtr;
    std::fstream fs;

    //To ensure no one else can instantiate Logger.
    Logger();

public:
    enum MessageType
    {
        ERROR,
        WARNING,
        INFO
    };

    static Logger* Instance();
    void LogFunc(std::string msg, MessageType type);

};

#endif /* Logger_hpp */

CPP 文件:

#include "Logger.hpp"
#include <iostream>

Logger* Logger::loggerPtr = NULL;

Logger* Logger::Instance()
{
    if (!loggerPtr)
    {
        loggerPtr = new Logger;
    }

    return loggerPtr;
}

Logger::Logger()
{
    fs.open("docker.log", std::fstream::in | std::fstream::out | std::fstream::app);
}

void Logger::LogFunc(std::string msg, MessageType type)
{
    std::cout << msg;

    switch (type)
    {
        case ERROR:
            fs << msg;
            break;
        case WARNING:
            fs << msg;
            break;
        case INFO:
            fs << msg;
            break;
    }
}

主文件:

int main() {

    Logger::Instance()->LogFunc("Hello.", Logger::INFO);

    std::fstream fs;
    fs.open("docker_test.log", std::fstream::in | std::fstream::out | std::fstream::app);
    fs << "Why does this work?!";

    system("pwd");

    return 0;
}

日志输出:

cat docker.log -> outputs H
cat docker_test.log -> outputs Why does this work?!

我错过了什么?提前致谢!

这可能与您没有 flush 您的 fstream 这一事实有关。由于它被缓冲,因此缓冲区不会写入文件。在代码的末尾, Logger 没有被破坏(使用原始指针的不便),缓冲区也没有被刷新。

它适用于您在 main() 中打开的流,因为本地流在离开该函数时会自动销毁(并关闭)。

建议:

fs << msg;   // for every logging output in syour switch 
fs.flush();  // add a flush

问题是你永远不会 delete 你的 单身人士 所以 std::fstream 永远不会被刷新。

我会对你的单例做一些不同的事情。我不使用 pointer,而是使用 instance() 函数的本地静态实例,如下所示:

#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>

class Logger
{
private:
    std::fstream fs;

    //To ensure no one else can instantiate Logger.
    Logger();

public:
    enum MessageType
    {
        ERROR,
        WARNING,
        INFO
    };

    static Logger& Instance(); // return reference
    void LogFunc(std::string msg, MessageType type);

};

Logger& Logger::Instance()
{
    // put the static in the function to ensure creation time
    static Logger logger; // use a static instance, not pointer
    return logger;
}

Logger::Logger()
{
    fs.open("docker.log", std::fstream::in | std::fstream::out | std::fstream::app);
}

void Logger::LogFunc(std::string msg, MessageType type)
{
    std::cout << msg;

    switch (type)
    {
        case ERROR:
            fs << msg;
            break;
        case WARNING:
            fs << msg;
            break;
        case INFO:
            fs << msg;
            break;
    }
}

int main() {

    Logger::Instance().LogFunc("Hello.", Logger::INFO);

    std::fstream fs;
    fs.open("docker_test.log", std::fstream::in | std::fstream::out | std::fstream::app);
    fs << "Why does this work?!";

    system("pwd");

    return 0;
}