Ofstream 创建但不会写入文件

Ofstream creates but wont write to file

我已经编写了一些代码来执行基本的 fizzbuzz 程序来测试我的日志记录 class,出于某种原因,数据被转储到控制台并且文件创建正常但是日志文件在任何时候都是空的我打开它。

我的主线在这里:

int main()
{
    logger* loggerObj = logger::createLogger("log.txt");

    for (int i = 1; i <= 100; i++)
    {
        loggerObj->createLogEvent(i);

        if (i == 15)
        {
           loggerObj->writeLog();
        }
}

这是我的 class:

int logger::m_instanceCount = 0;
logger* logger::loggerObj = new logger;
string logger::m_fileName = "log.txt";

logger::logger()
{
}

logger::~logger()
{
}

logger* logger::createLogger(string fileName)
{
    if (m_instanceCount == 0)
    {
        loggerObj = new logger;
        m_fileName = fileName;

        return loggerObj;
    }
    else
    {
        return loggerObj;
    }
}

bool logger::addLogToQueue(logEvent event)
{
    if (m_queueID == 15)
    {
        return false;
    }
    else
    {
        queue[m_queueID] = event;
        m_queueID++;
        return true;
    }
}

void logger::logQueue()
{
    for (int i = 0; i <= m_queueID; i++)
    {
        int level = getEventLevel(i);
        string message = getEventMessage(i);

        writeLog();
    }
}

int logger::getEventLevel(int logID)
{
    return queue[logID].logLevel;
}

string logger::getEventMessage(int logID)
{
    return queue[logID].logMessage;
}

void logger::writeLog()
{
    ofstream log(m_fileName, ios::out);
    log.open(m_fileName, ios::out);
    log.flush();
    string prefix;
    int level;
    string message;
    int queueSize = m_queueID;

    for (int i = 0; i <= queueSize - 1; i++)
    {
        level = queue[i].logLevel;
        message = queue[i].logMessage;

        switch (level)
        {
        case 1:
            prefix = "[Fizz] ";
            break;
        case 2:
            prefix = "[Buzz] ";
            break;
        case 3:
            prefix = "[FizzBuzz] ";
            break;
        default:
            prefix = "[Number] ";
            break;
        }

        string fullMessage = prefix.append(message);
        cout << fullMessage << endl;
        log << fullMessage << endl;

        m_queueID--;
    }

    log.close();
}

void logger::createLogEvent(int number)
{
    logEvent event;
    if (number % 3 == 0 && number % 5 == 0)
    {
        event.logLevel = 3;
        event.logMessage = to_string(number);
    }
    else if (number % 3 == 0)
    {
        event.logLevel = 1;
        event.logMessage = to_string(number);
    }
    else if (number % 5 == 0)
    {
        event.logLevel = 2;
        event.logMessage = to_string(number);
    }
    else
    {
        event.logMessage = to_string(number);
    }

    addLogToQueue(event);
}

这是我的 class header:

#ifndef LOGGER_INCLUDED
#define LOGGER_INCLUDED

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class logger
{
public:
    ~logger();
    struct logEvent;
    static logger* createLogger(string);
    bool addLogToQueue(logEvent);
    void logQueue();
    void createLogEvent(int);
    int getEventLevel(int);
    string getEventMessage(int);
    void writeLog();
private:
    logger();
    struct logEvent
    {
        string logMessage;
        int logLevel;
    };
    static int m_instanceCount;
    static logger* loggerObj;
    static string m_fileName;
    logEvent queue[15];
    int m_queueID = 0;
};

#endif

这是我第一次尝试登录,所以如果是一个简单的错误,请原谅我。提前致谢。此外,这并不是设计为最好或最有效的,它只是训练,因此使用单身人士和 queues 并不是真正需要的,只是尝试一些东西

现在已经修复了,我不知道它被打开了两次。

ofstream log(m_fileName, ios::out);
log.open(m_fileName, ios::out);

构造函数打开文件。

紧接着,第二行再次尝试 open() 文件。那是一个错误。文件流的std::failbit已设置,后续所有写操作均失败。