From 69997c8a0843f7ad1bb72201f9474d8b5c0214db Mon Sep 17 00:00:00 2001 From: lichunjun Date: Sat, 28 Jan 2023 10:53:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(util):=E8=A7=A3=E5=86=B3fs::WriteStringToTe?= =?UTF-8?q?xtFile()=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.通过在WriteStringToTextFile()中添加flush_now的方式决定是否需要执行flush操作 Fixes:#I6BARW --- modules/util/fs.cpp | 8 +++++--- modules/util/fs.h | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/modules/util/fs.cpp b/modules/util/fs.cpp index 82aff8b..cf09b46 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 8f4e6e5..4c06ac9 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); //////////////////////////////////////////////////////////////////// -- Gitee