C++ C++;fstream仅保存字符串的第一个字符
正如标题所说,我对fstream有一个问题。我创建了一个单例日志类,它使用fstream将日志消息保存在一个文件中 我使用idexcode。在VisualStudio2013中,代码运行良好 头文件:C++ C++;fstream仅保存字符串的第一个字符,c++,xcode,visual-studio,fstream,C++,Xcode,Visual Studio,Fstream,正如标题所说,我对fstream有一个问题。我创建了一个单例日志类,它使用fstream将日志消息保存在一个文件中 我使用idexcode。在VisualStudio2013中,代码运行良好 头文件: #ifndef Logger_hpp #define Logger_hpp #include <stdio.h> #include <fstream> #include <string> class Logger { private: static
#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 */
我错过了什么?提前谢谢 这可能与您不使用
fstream
有关。当缓冲区被缓冲时,缓冲区就不会写入文件。在代码结束时,记录器
不会被破坏(使用原始指针的不便),缓冲区也不会被刷新
它适用于您在main()
中打开的流,因为本地流在离开函数时会自动销毁(并关闭)
建议:
fs << msg; // for every logging output in syour switch
fs.flush(); // add a flush
fs问题是你永远不会删除你的单身汉,所以std::fstream
永远不会被刷新
我会对你的单身汉有点不同。我不使用指针,而是使用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;
}
#包括
#包括
#包括
#包括
类记录器
{
私人:
std::fstream fs;
//以确保没有其他人可以实例化记录器。
记录器();
公众:
枚举消息类型
{
错误,
警告
信息
};
静态记录器&Instance();//返回引用
void LogFunc(std::string msg,MessageType类型);
};
记录器&记录器::实例()
{
//在函数中放入static以确保创建时间
静态记录器;//使用静态实例,而不是指针
返回记录器;
}
记录器::记录器()
{
打开(“docker.log”,std::fstream::in | std::fstream::out | std::fstream::app);
}
void Logger::LogFunc(std::string msg,MessageType)
{
std::cout文本输出必须以换行符结束。在每次插入的末尾插入'\n'
。这很有效!感谢添加的信息Christophe。不客气。另一种可能是稍微更改代码以使用一个而不是一个原始指针,以确保它在结尾处关闭。另一方面,刷新具有优势写入文件的年龄,即使你的应用程序在操作过程中崩溃。
cat docker.log -> outputs H
cat docker_test.log -> outputs Why does this work?!
fs << msg; // for every logging output in syour switch
fs.flush(); // add a flush
#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;
}