bool cBinaryFile::Openfile(const char*e_strFileName,const char*e_strformat) { this->m_pFile = MyFileOpen(e_strFileName,e_strformat ); if(m_pFile) return true; return false; }
bool cBinaryFile::Writefile(const char*e_str,bool e_bBinary,bool e_bForceToWrite) { CloseFile(); #ifdef WIN32 if( e_bForceToWrite ) { int l_iFlag = FILE_ATTRIBUTE_NORMAL; if( e_bForceToWrite ) l_iFlag |= (FILE_FLAG_WRITE_THROUGH); m_FileHandle = CreateFile(UT::CharToWchar(e_str).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, l_iFlag, nullptr); if (m_FileHandle != INVALID_HANDLE_VALUE) { int file_descriptor = _open_osfhandle((intptr_t)m_FileHandle, 0); if (file_descriptor != -1) { if( e_bBinary ) m_pFile = _fdopen(file_descriptor, "wb+");//b for windows only else this->m_pFile = _fdopen(file_descriptor,"w"); } } } else { if( e_bBinary ) this->m_pFile = MyFileOpen(e_str,"wb+"); else this->m_pFile = MyFileOpen(e_str,"w"); } #else std::string l_strFileName = e_str; if( e_bBinary ) this->m_pFile = MyFileOpen(l_strFileName.c_str(),"wb+" ); else this->m_pFile = MyFileOpen(l_strFileName.c_str(),"w" ); #endif if(m_pFile) return true; return false; }
// ===================================================================================================================== // str为要打开的文件名(包含路径) 例如 debug1/debug2/debug3/debug.log // ===================================================================================================================== CFileLogWriter::CFileLogWriter(const std::string &str) { // 先创建文件夹 size_t pos = str.find('/'); std::string strTemp; while (pos != std::string::npos) { strTemp = str.substr(0, pos); MyCreateDirectory(strTemp.c_str()); pos = str.find('/', pos + 1); } m_fp = MyFileOpen(str.c_str(), "a+"); }