From d1287fe2342dcbe4cc9dffd9a505bb5397901aeb Mon Sep 17 00:00:00 2001 From: Quella777 <2892744389@qq.com.com> Date: Mon, 11 Aug 2025 11:40:28 +0800 Subject: [PATCH] code update --- README.md | 12 ++++++++++++ include/Netra.hpp | 6 ++++-- src/Netra.cpp | 20 ++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 49bef21..b1e08cc 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,15 @@ c/c++基本开发库 可以计算特定符号最后一个字节所在位置 所有操作都添加mutex锁机制 ,保障线程安全 + + +# 读文件操作 +支持全文读取(文本和二进制模式) +支持按行读取文本内容 +支持按指定字节数读取数据 +支持计算第一个指定字节序列结束位置(包含该序列本身)的字节数 +提供文件是否存在和文件大小查询 +支持重置文件读取位置,实现多次读取 + + + diff --git a/include/Netra.hpp b/include/Netra.hpp index 142e263..8e6b1bf 100644 --- a/include/Netra.hpp +++ b/include/Netra.hpp @@ -198,6 +198,7 @@ namespace QCL * - 与 WriteFile 类风格保持一致 * - 支持文本文件与二进制文件 * - 自动关闭文件(析构时) + * - 内部使用 std::mutex 实现线程安全 */ class ReadFile { @@ -278,8 +279,9 @@ namespace QCL void Reset(); private: - std::string filename_; // 文件路径 - std::ifstream file_; // 文件流对象 + std::string filename_; // 文件路径 + std::ifstream file_; // 文件流对象 + mutable std::mutex mtx_; // 可变,保证 const 方法也能加锁 }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/Netra.cpp b/src/Netra.cpp index e6c299f..eac64bf 100644 --- a/src/Netra.cpp +++ b/src/Netra.cpp @@ -344,11 +344,15 @@ namespace QCL ReadFile::~ReadFile() { + std::lock_guard lock(mtx_); Close(); } bool ReadFile::Open() { + std::lock_guard lock(mtx_); + if (file_.is_open()) + file_.close(); file_.open(filename_, std::ios::in | std::ios::binary); return file_.is_open(); } @@ -356,16 +360,21 @@ namespace QCL void ReadFile::Close() { if (file_.is_open()) + { + std::lock_guard lock(mtx_); file_.close(); + } } bool ReadFile::IsOpen() const { + std::lock_guard lock(mtx_); return file_.is_open(); } std::string ReadFile::ReadAllText() { + std::lock_guard lock(mtx_); if (!file_.is_open() && !Open()) return ""; @@ -376,6 +385,7 @@ namespace QCL std::vector ReadFile::ReadAllBinary() { + std::lock_guard lock(mtx_); if (!file_.is_open() && !Open()) return {}; @@ -384,6 +394,7 @@ namespace QCL std::vector ReadFile::ReadLines() { + std::lock_guard lock(mtx_); if (!file_.is_open() && !Open()) return {}; @@ -398,17 +409,19 @@ namespace QCL std::vector ReadFile::ReadBytes(size_t count) { + std::lock_guard lock(mtx_); if (!file_.is_open() && !Open()) return {}; std::vector buffer(count); file_.read(buffer.data(), count); - buffer.resize(file_.gcount()); // 实际读取的字节数 + buffer.resize(file_.gcount()); return buffer; } size_t ReadFile::GetBytesBefore(const std::string &marker) { + std::lock_guard lock(mtx_); if (!file_.is_open() && !Open()) return 0; @@ -425,11 +438,13 @@ namespace QCL bool ReadFile::FileExists() const { + std::lock_guard lock(mtx_); return std::filesystem::exists(filename_); } size_t ReadFile::GetFileSize() const { + std::lock_guard lock(mtx_); if (!FileExists()) return 0; return std::filesystem::file_size(filename_); @@ -437,9 +452,10 @@ namespace QCL void ReadFile::Reset() { + std::lock_guard lock(mtx_); if (file_.is_open()) { - file_.clear(); // 清除 EOF 标志 + file_.clear(); file_.seekg(0, std::ios::beg); } }