1 class LogFile 2 { 3 public: 4 static LogFile &instance(); 5 operator FILE *() const { return m_file; } 6 private: 7 LogFile(const char *filename) 8 { 9 m_file = fopen(filename, "a+"); 10 } 11 ~LogFile() 12 { 13 fclose(m_file); 14 } 15 FILE *m_file; 16 }; 17 18 LogFile &LogFile::instance() 19 { 20 static LogFile log("AppLog.txt"); 21 return log; 22 } 23 24 //用的时候可以这么写: 25 fwrite("abc", 1, 3, LogFile::instance());
时间: 2024-11-07 19:07:05