2026-03-18 17:49:22 +08:00
|
|
|
|
# FileWriter
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Core`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `class`
|
|
|
|
|
|
|
2026-03-20 02:35:07 +08:00
|
|
|
|
**头文件**: `XCEngine/Core/FileWriter.h`
|
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
|
**描述**: 文件写入工具类,提供简单的文件写入操作封装。
|
|
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
|
|
`FileWriter` 是一个轻量级的文件写入工具,封装了 `FILE*` 接口,提供便捷的字符串和二进制数据写入功能。它支持追加模式和自动资源管理(RAII)。
|
|
|
|
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
docs: fix naming conventions across threading, math, memory, core, and debug modules
threading/:
- Rename 19 camelCase method files to hyphenated names
- task-system: createtaskgroup→create-task-group, etc.
- tasksystemconfig: enabletaskprofiling→enable-task-profiling, etc.
- thread: getcurrentid→get-current-id, etc.
- task: addref→add-ref, getid→get-id, etc.
math/:
- Rename underscore operator files to hyphenated
- vector3: operator_add→operator-add, etc.
- matrix4: gettranslation→get-translation, etc.
- vector4: tovector3→to-vector3, constructor_vector3→constructor-vector3
- sphere: sphere_constructor→sphere-constructor, etc.
memory/:
- Remove duplicate memorymanager/ folder (kept manager/ which was correct)
core/:
- filewriter: Consolidate ctor-default.md and ctor-file.md into constructor.md
- Rename dtor.md→destructor.md
debug/:
- filelogsink: Rename construct.md→constructor.md, ~filelogsink.md→destructor.md
All overview pages updated with new file references.
2026-03-22 23:09:29 +08:00
|
|
|
|
| [`FileWriter()`](constructor.md) | 默认构造(不打开文件) |
|
|
|
|
|
|
| [`FileWriter(filePath, append)`](constructor.md) | 构造并打开文件 |
|
|
|
|
|
|
| [`~FileWriter()`](destructor.md) | 析构函数,自动关闭文件 |
|
2026-03-19 02:01:18 +08:00
|
|
|
|
| [`Open`](Open.md) | 打开文件,append=true 时为追加模式 |
|
|
|
|
|
|
| [`Close`](Close.md) | 关闭文件 |
|
|
|
|
|
|
| [`IsOpen`](IsOpen.md) | 检查文件是否已打开 |
|
|
|
|
|
|
| [`Write`](Write.md) | 写入数据 |
|
|
|
|
|
|
| [`Flush`](Flush.md) | 刷新缓冲区 |
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
|
#include <XCEngine/Core/FileWriter.h>
|
2026-03-26 01:58:45 +08:00
|
|
|
|
#include <XCEngine/Core/Containers/String.h>
|
2026-03-19 01:04:30 +08:00
|
|
|
|
|
|
|
|
|
|
using namespace XCEngine::Core;
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 方式1:构造时打开
|
|
|
|
|
|
FileWriter writer("output.txt", false);
|
|
|
|
|
|
if (writer.IsOpen()) {
|
|
|
|
|
|
writer.Write("Hello, World!\n");
|
|
|
|
|
|
writer.Write("Line 2\n");
|
|
|
|
|
|
writer.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 方式2:先构造再打开
|
|
|
|
|
|
FileWriter writer2;
|
|
|
|
|
|
if (writer2.Open("log.txt")) {
|
|
|
|
|
|
writer2.Write("Application started\n");
|
|
|
|
|
|
writer2.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 追加模式
|
|
|
|
|
|
FileWriter appendWriter("log.txt", true);
|
|
|
|
|
|
if (appendWriter.IsOpen()) {
|
|
|
|
|
|
appendWriter.Write("Another log entry\n");
|
|
|
|
|
|
appendWriter.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 String 写入
|
|
|
|
|
|
Containers::String content = "Content written from String";
|
|
|
|
|
|
FileWriter writer3("data.txt");
|
|
|
|
|
|
writer3.Write(content);
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
- [Core 模块总览](../core.md) - 返回模块总览
|
|
|
|
|
|
- [Logger](../../debug/logger/logger.md) - 日志系统
|
|
|
|
|
|
- [FileLogSink](../../debug/filelogsink/filelogsink.md) - 文件日志输出
|