Ofstream creates but wont write to file

天大地大妈咪最大 提交于 2019-12-31 07:34:06

问题


I have wrote some code that does a basic fizzbuzz program to test my logging class, for some reason the data is dumped to the console fine and the file is created fine however the log file is empty whenever I open it.

My main is here:

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

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

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

and this is my 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);
}

this is my 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

This is my first time trying logging so if its a simple mistake please forgive me. Thanks in advance. Also this is not designed to be the best or most efficient, its merely training hence the use of singletons and queues that aren't really needed, just trying some stuff out


回答1:


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

The constructor opens the file.

Immediately afterwards, the second line attempts to open() the file again. That's an error. The file stream's std::failbit is set, and all subsequent write operations fail.




回答2:


This has been fixed now, i wasnt aware it was being opened twice.



来源:https://stackoverflow.com/questions/37471925/ofstream-creates-but-wont-write-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!