56 using Bytes = std::vector<unsigned char>;
63 Path(
const std::string &str) : path(str)
83 return p1.path +
'/' + p2.path;
93 std::ifstream fin(path);
95 throw file_error(
"Cannot Open File \"" + path +
'"');
97 std::istreambuf_iterator<char>(fin),
98 std::istreambuf_iterator<char>()};
100 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
111 std::ifstream fin(path, std::ios::binary);
113 throw file_error(
"Cannot Open File \"" + path +
'"');
115 std::istreambuf_iterator<char>(fin),
116 std::istreambuf_iterator<char>()};
118 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
142 static_assert(std::is_trivially_copyable<T>::value,
143 "This Type is Not POD Type!");
144 std::ifstream fin(path, std::ios::binary);
146 throw file_error(
"Cannot Open File \"" + path +
'"');
148 fin.read((
char *)(&data),
sizeof(data));
150 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
164 std::ifstream fin(path, std::ios::binary);
166 throw file_error(
"Cannot Open File \"" + path +
'"');
168 fin.read((
char *)(&data),
sizeof(data));
170 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
181 std::ofstream fout(path);
183 throw file_error(
"Cannot Open File \"" + path +
'"');
186 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
196 std::ofstream fout(path, std::ios::binary);
198 throw file_error(
"Cannot Open File \"" + path +
'"');
199 fout.write((
const char *)(bts.data()), bts.size());
201 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
212 std::ofstream fout(path, std::ios::binary);
216 throw file_error(
"Cannot Open File \"" + path +
'"');
217 auto it = lines.begin();
219 while (++it != lines.end())
223 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
236 static_assert(std::is_trivially_copyable<T>::value,
237 "This Type is Not POD Type!");
238 std::ofstream fout(path, std::ios::binary);
240 throw file_error(
"Cannot Open File \"" + path +
'"');
241 fout.write((
const char *)(&data),
sizeof(data));
243 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
256 std::ofstream fout(path, std::ios::binary);
258 throw file_error(
"Cannot Open File \"" + path +
'"');
259 fout.write((
const char *)(&data),
sizeof(data));
261 throw file_error(
"The Stream of \"" + path +
"\" is Not Good");
270 return std::ifstream{path}.is_open();
288 std::ofstream{path, std::ios::app};
T unsafe_read_POD()
从二进制文件读取一个 POD 类型对象(不安全版本)。
Definition file.h:162
std::vector< std::string > read_lines()
按行读取文本文件,返回每行字符串的 vector。
Definition file.h:127
void write_text(const std::string &text)
以文本模式写入字符串到文件(覆盖模式)。
Definition file.h:179
void write_lines(const std::vector< std::string > &lines)
将多行字符串写入文件,每行之间用换行符分隔。
Definition file.h:210
Path(const std::string &str)
从字符串构造 Path 对象。
Definition file.h:63
void touch()
创建空文件(若已存在则更新访问和修改时间)。
Definition file.h:277
void ensure()
确保文件存在,若不存在则创建空文件。
Definition file.h:286
void write_POD(const T &data)
写入一个 POD 类型对象到二进制文件(类型安全版本)。
Definition file.h:234
bool exists()
检查文件是否存在。
Definition file.h:268
std::vector< unsigned char > Bytes
字节类型别名,表示二进制数据的容器(unsigned char 的 vector)。
Definition file.h:56
T read_POD()
从二进制文件读取一个 POD 类型对象(类型安全版本)。
Definition file.h:140
std::string read_text()
以文本模式读取文件全部内容。
Definition file.h:91
void unsafe_write_POD(const T &data)
写入一个对象到二进制文件(不安全版本)。
Definition file.h:254
Bytes read_binary()
以二进制模式读取文件全部内容。
Definition file.h:109
friend Path operator/(const Path &p1, const Path &p2)
路径拼接运算符。
Definition file.h:81
void write_binary(const Bytes &bts)
以二进制模式写入字节数据到文件(覆盖模式)。
Definition file.h:194
表示文件操作错误,如打开失败、读取失败等。
Definition csexc.h:89
定义 console 库使用的自定义异常类层次结构。
std::vector< std::string > split(std::string text, const std::string &sep=" ")
以分隔符分割字符串(类似 Python 的 split,默认按空格分割)。
Definition strpp.h:237