diff --git a/modules/util/fs.cpp b/modules/util/fs.cpp index 82aff8b0300a74376852b0694dbfb8d9fa661709..cf09b469f0b98a87293214a6cb978d3ec110b054 100644 --- a/modules/util/fs.cpp +++ b/modules/util/fs.cpp @@ -45,13 +45,15 @@ bool ReadStringFromTextFile(const std::string &filename, std::string &content) return false; } -bool WriteStringToTextFile(const std::string &filename, const std::string &content) +bool WriteStringToTextFile(const std::string &filename, const std::string &content, bool flush_now) { ofstream ofs; try { ofs.open(filename, ofstream::out | ofstream::trunc); if (ofs.is_open()) { ofs << content; + if (flush_now) + ofs << std::flush; return true; } else { LogWarn("open failed, %s", filename.c_str()); @@ -67,9 +69,9 @@ bool ReadBinaryFromFile(const std::string &filename, std::string &content) return ReadStringFromTextFile(filename, content); } -bool WriteBinaryToFile(const std::string &filename, const std::string &content) +bool WriteBinaryToFile(const std::string &filename, const std::string &content, bool flush_now) { - return WriteStringToTextFile(filename, content); + return WriteStringToTextFile(filename, content, flush_now); } bool RemoveFile(const std::string &filename, bool allow_log_print) diff --git a/modules/util/fs.h b/modules/util/fs.h index 8f4e6e56aa482b594ed555a90174a9306bbde194..4c06ac9cb339cb9ccbc6e93c77e4d0d9bd9d8679 100644 --- a/modules/util/fs.h +++ b/modules/util/fs.h @@ -34,8 +34,15 @@ bool ReadStringFromTextFile(const std::string &filename, std::string &content); /** * 将字串写入到文件 + * + * \param filename 文件名 + * \param content 将要写入的字串内容 + * \param flush_now 是否需要立即flush + * + * \return true 成功 + * \return false 失败 */ -bool WriteStringToTextFile(const std::string &filename, const std::string &content); +bool WriteStringToTextFile(const std::string &filename, const std::string &content, bool flush_now = false); /** * 从文件中读取数据 @@ -50,8 +57,15 @@ bool ReadBinaryFromFile(const std::string &filename, std::string &content); /** * 将数据写入到文件 + * + * \param filename 文件名 + * \param content 将要写入的数据内容 + * \param flush_now 是否需要立即flush + * + * \return true 成功 + * \return false 失败 */ -bool WriteBinaryToFile(const std::string &filename, const std::string &content); +bool WriteBinaryToFile(const std::string &filename, const std::string &content, bool flush_now = false); /** * 删除文件 @@ -63,8 +77,26 @@ bool WriteBinaryToFile(const std::string &filename, const std::string &content); */ bool RemoveFile(const std::string &filename, bool allow_log_print = true); +/// 创建符号链接文件 +/** + * \param old_path 源路径 + * \param new_path 符号链接文件的路径 + * \param allow_log_print 是否允许错误日志打印 + * + * \return true 成功 + * \return false 失败 + */ bool MakeSymbolLink(const std::string &old_path, const std::string &new_path, bool allow_log_print = true); +/// 创建链接文件 +/** + * \param old_path 源路径 + * \param new_path 符号链接文件的路径 + * \param allow_log_print 是否允许错误日志打印 + * + * \return true 成功 + * \return false 失败 + */ bool MakeLink(const std::string &old_path, const std::string &new_path, bool allow_log_print = true); ////////////////////////////////////////////////////////////////////