|
ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
|
文件读取类。 More...
#include <tools_file.h>
Public Member Functions | |
| readfile (const std::string &filename) | |
| 构造函数,打开指定文件。 | |
| ~readfile () | |
| 析构函数,自动关闭文件。 | |
| bool | is_open () const |
| 查询文件是否成功打开。 | |
| int | get_maxline () |
| 获取文件总行数。 | |
| void | jumpbegin (int anum, bool aiscsv) |
| 跳过文件开头的指定行数。 | |
| bool | readline (std::string &aline) |
| 读取一行。 | |
| bool | readcurrent (std::string &astr) |
| 从当前读取位置读取到文件末尾。 | |
| void | read (std::string &aneirong) |
| 从文件开头读取完整内容。 | |
文件读取类。
封装std::ifstream,提供按行读取、整文件读取、 行数查询等功能。支持CSV模式正确处理引号内的换行符。
|
explicit |
构造函数,打开指定文件。
构造函数,打开文件进行读取
| filename | 文件路径 |
| filename | 要打开的文件名 |
以二进制模式打开文件,避免换行符转换
| ngl::tools::readfile::~readfile | ( | ) |
析构函数,自动关闭文件。
析构函数,确保文件被正确关闭
使用try-catch确保析构函数不会抛出异常
| int ngl::tools::readfile::get_maxline | ( | ) |
获取文件总行数。
获取文件总行数
第一次调用时计算并缓存结果,后续调用直接返回缓存值 计算完成后恢复文件指针到原始位置,保持无副作用
| bool ngl::tools::readfile::is_open | ( | ) | const |
查询文件是否成功打开。
检查文件是否成功打开
| void ngl::tools::readfile::jumpbegin | ( | int | anum, |
| bool | aiscsv ) |
跳过文件开头的指定行数。
跳过文件开头的指定行数
| anum | 要跳过的行数 |
| aiscsv | 是否为CSV模式(CSV模式会正确处理引号内的换行符) |
| anum | 要跳过的行数 |
| aiscsv | 是否为CSV格式(处理引号内的换行) |
CSV模式下,引号内的换行不计入行数 非CSV模式下,简单跳过指定行数
| void ngl::tools::readfile::read | ( | std::string & | aneirong | ) |
从文件开头读取完整内容。
读取整个文件内容
| aneirong | 输出参数,存储文件全部内容 |
| aneirong | [out] 存储文件全部内容 |
先获取文件大小,然后一次性读取 如果文件为空,aneirong会被清空
| bool ngl::tools::readfile::readcurrent | ( | std::string & | astr | ) |
从当前读取位置读取到文件末尾。
从当前位置读取到文件末尾
| astr | 输出参数,存储读取的内容 |
| astr | [in/out] 存储读取的内容(追加到末尾) |
不会清空astr原有内容,而是追加新内容 用于增量读取文件
| bool ngl::tools::readfile::readline | ( | std::string & | aline | ) |
读取一行。
读取一行内容
| aline | 输出参数,存储读取的行内容 |
| aline | [out] 存储读取的行内容 |
自动去除行尾的'\r'字符(Windows换行符)