Ofstream创建但不会写入文件

时间:2016-05-26 22:14:23

标签: c++ file logging ofstream

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

我的主要在这里:

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

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

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

这是我的班级:

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);
}

这是我的班级标题:

#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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

构造函数打开文件。

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