文件路径封装类,提供便捷的文件读写和路径操作。
More...
#include <file.h>
|
| using | Bytes = std::vector<unsigned char> |
| | 字节类型别名,表示二进制数据的容器(unsigned char 的 vector)。
|
|
| | Path (const std::string &str) |
| | 从字符串构造 Path 对象。
|
| std::string | read_text () |
| | 以文本模式读取文件全部内容。
|
| Bytes | read_binary () |
| | 以二进制模式读取文件全部内容。
|
| std::vector< std::string > | read_lines () |
| | 按行读取文本文件,返回每行字符串的 vector。
|
| template<class T> |
| T | read_POD () |
| | 从二进制文件读取一个 POD 类型对象(类型安全版本)。
|
| template<class T> |
| T | unsafe_read_POD () |
| | 从二进制文件读取一个 POD 类型对象(不安全版本)。
|
| void | write_text (const std::string &text) |
| | 以文本模式写入字符串到文件(覆盖模式)。
|
| void | write_binary (const Bytes &bts) |
| | 以二进制模式写入字节数据到文件(覆盖模式)。
|
| void | write_lines (const std::vector< std::string > &lines) |
| | 将多行字符串写入文件,每行之间用换行符分隔。
|
| template<class T> |
| void | write_POD (const T &data) |
| | 写入一个 POD 类型对象到二进制文件(类型安全版本)。
|
| template<class T> |
| void | unsafe_write_POD (const T &data) |
| | 写入一个对象到二进制文件(不安全版本)。
|
| bool | exists () |
| | 检查文件是否存在。
|
| void | touch () |
| | 创建空文件(若已存在则更新访问和修改时间)。
|
| void | ensure () |
| | 确保文件存在,若不存在则创建空文件。
|
文件路径封装类,提供便捷的文件读写和路径操作。
在 Windows 平台上自动将 '/' 转换为 '\',支持路径拼接运算符 /。 所有文件读写操作均会抛出 file_error 异常(若文件无法打开或流状态异常)。
◆ Bytes
字节类型别名,表示二进制数据的容器(unsigned char 的 vector)。
◆ Path()
| console::Path::Path |
( |
const std::string & | str | ) |
|
|
inline |
从字符串构造 Path 对象。
- Parameters
-
- Note
- 在 Windows 上会将 '/' 自动转换为 '\'。
◆ ensure()
| void console::Path::ensure |
( |
| ) |
|
|
inline |
确保文件存在,若不存在则创建空文件。
使用追加模式打开文件,不会清空已有内容。
◆ exists()
| bool console::Path::exists |
( |
| ) |
|
|
inline |
检查文件是否存在。
- Returns
- bool 若文件存在且可打开则返回 true,否则 false。
◆ read_binary()
| Bytes console::Path::read_binary |
( |
| ) |
|
|
inline |
以二进制模式读取文件全部内容。
- Returns
- Bytes 无符号字节向量。
- Exceptions
-
◆ read_lines()
| std::vector< std::string > console::Path::read_lines |
( |
| ) |
|
|
inline |
按行读取文本文件,返回每行字符串的 vector。
- Returns
- std::vector<std::string> 各行内容(不包含换行符)。
- Exceptions
-
◆ read_POD()
template<class T>
| T console::Path::read_POD |
( |
| ) |
|
|
inline |
从二进制文件读取一个 POD 类型对象(类型安全版本)。
- Template Parameters
-
| T | 必须是平凡可复制(trivially copyable)的类型。 |
- Returns
- T 读取到的对象。
- Exceptions
-
- Note
- 编译期检查 T 是否为 POD 类型,否则触发 static_assert。
◆ read_text()
| std::string console::Path::read_text |
( |
| ) |
|
|
inline |
以文本模式读取文件全部内容。
- Returns
- std::string 文件内容。
- Exceptions
-
◆ touch()
| void console::Path::touch |
( |
| ) |
|
|
inline |
创建空文件(若已存在则更新访问和修改时间)。
相当于 Unix 的 touch 命令,若文件不存在则创建,若存在则仅更新时间戳。
◆ unsafe_read_POD()
template<class T>
| T console::Path::unsafe_read_POD |
( |
| ) |
|
|
inline |
从二进制文件读取一个 POD 类型对象(不安全版本)。
- Template Parameters
-
- Returns
- T 读取到的对象。
- Exceptions
-
- Warning
- 不检查 T 是否为 POD 类型,可能因类型不匹配导致未定义行为。
◆ unsafe_write_POD()
template<class T>
| void console::Path::unsafe_write_POD |
( |
const T & | data | ) |
|
|
inline |
写入一个对象到二进制文件(不安全版本)。
- Template Parameters
-
- Parameters
-
- Exceptions
-
- Warning
- 不检查 T 是否为 POD 类型,直接以二进制写入内存表示,可能导致不可移植。
◆ write_binary()
| void console::Path::write_binary |
( |
const Bytes & | bts | ) |
|
|
inline |
以二进制模式写入字节数据到文件(覆盖模式)。
- Parameters
-
- Exceptions
-
◆ write_lines()
| void console::Path::write_lines |
( |
const std::vector< std::string > & | lines | ) |
|
|
inline |
将多行字符串写入文件,每行之间用换行符分隔。
- Parameters
-
- Exceptions
-
- Note
- 若 lines 为空,则不做任何操作(不创建文件)。
◆ write_POD()
template<class T>
| void console::Path::write_POD |
( |
const T & | data | ) |
|
|
inline |
写入一个 POD 类型对象到二进制文件(类型安全版本)。
- Template Parameters
-
- Parameters
-
- Exceptions
-
- Note
- 编译期检查 T 是否为 POD 类型。
◆ write_text()
| void console::Path::write_text |
( |
const std::string & | text | ) |
|
|
inline |
以文本模式写入字符串到文件(覆盖模式)。
- Parameters
-
- Exceptions
-
◆ operator/
路径拼接运算符。
- Parameters
-
- Returns
- Path 拼接后的新路径,格式为 "p1/p2" 或 "p1\\p2"。
- Note
- 在 Windows 拼接后,构造函数会自动将 '/' 变成 '\'。
The documentation for this class was generated from the following file: