std :: ofstream有时不会写入文件 [英] std::ofstream won't write to file (sometimes)

查看:334
本文介绍了std :: ofstream有时不会写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了许多具有相同或相似标题的问题,我已经以许多我无法数数的方式更改了代码....我有一个有趣的问题.
我有一个非常简单的日志记录类,只是将内容写入文件中.完全相同的代码在构造函数中有效,但在成员函数中无效.我正在剥离一些不相关的代码,其余的是:

I've gone over many questions with the same or similar titles, I have changed the code in so many ways I can't even count.... I have an interesting problem.
I have a class for logging that is extremely simple and just writes stuff into a file. The exact same code works in the constructor, but will not work in the member function. I'm stripping out some irrelevant code, the rest is:

private:
    std::string logfile_path_;
    std::string program_name_;
    std::string GetTimestamp() {
        timeval tv;
        gettimeofday(&tv, NULL);
        char cTimestamp[24];
        strftime(cTimestamp, sizeof(cTimestamp), "%F %T", std::localtime(&tv.tv_sec));
        sprintf(&cTimestamp[19], ".%03d", (tv.tv_usec / 1000));         // write the miliseconds (microseconds/1000) into cTimestamp starting at the 20th character. %03d == pad with 0, for a minimum length of 3, an integer.
        return cTimestamp;      // function returns std::string so this will be implicitly cast into a string and returned.
    }

public:
    int log_level_;

    SrxDsLog(std::string Logfile_path, std::string program_name, int log_level) {
        log_level_ = log_level;
        program_name_ = program_name;
        logfile_path_ = Logfile_path;
        std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
        std::cout << "Logger started, Log file: " << logfile_path_ << std::endl;
        logfile << "Logger started, Log file: " << logfile_path_ << std::endl;
        return;
    }

    void WriteLog(std::string Log_message, int Severity = LOG_CRITICAL, std::string Function_name = "") {
        if (Severity >= log_level_) {
            std::cout << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
            std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
            logfile << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
        }
    }

问题是为什么它在构造函数中起作用,但是完全相同的代码在成员函数中却不起作用. std :: cout正在写我想要的完全相同的日志消息,但是它没有出现在文件中.每次运行程序时,该文件仅包含一行.

The question is why is it working in the constructor, but the exact same code is not working in the member function. The std::cout is writing the exact same log message I want, but it's not appearing in the file. The file contains a single line every time the program is run.

推荐答案

在令人惊讶的一系列事件中,我投票结束了我的问题.
该问题显然是由无关代码中未定义的行为引起的.那是因为我做了C ++ 11中定义但不在C ++ 03中定义的操作.显然,您不能从C ++ 03中的构造函数调用构造函数. 因此,并且由于该问题未包含实际上有错误的代码,因此该问题似乎非常糟糕.

In an amazingly unsatisfying turn of events I voted to close my question.
The problem was apparently caused by undefined behavior in unrelated code. And that because I did something that's defined in C++11 but is not in C++03. Apparently you can't call constructors from constructors in C++03....
Because of that, and because the question didn't include the code that was actually at fault, the question seems incredibly bad.

请关闭.

这篇关于std :: ofstream有时不会写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆