49 lines
988 B
Markdown
49 lines
988 B
Markdown
|
|
# FileWriter::Write
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
bool Write(const char* data, size_t length);
|
||
|
|
bool Write(const Containers::String& str);
|
||
|
|
```
|
||
|
|
|
||
|
|
写入数据到文件。
|
||
|
|
|
||
|
|
**描述**
|
||
|
|
|
||
|
|
将数据写入文件。如果文件未打开,操作会失败。
|
||
|
|
|
||
|
|
**参数:**
|
||
|
|
- `data` - 要写入的字符数据
|
||
|
|
- `length` - 要写入的字节数
|
||
|
|
- `str` - 要写入的 String 对象
|
||
|
|
|
||
|
|
**返回:** `bool` - 是否写入成功
|
||
|
|
|
||
|
|
**复杂度:** O(n) 其中 n 为写入的字节数
|
||
|
|
|
||
|
|
**示例:**
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
#include <XCEngine/Core/FileWriter.h>
|
||
|
|
#include <XCEngine/Containers/String.h>
|
||
|
|
|
||
|
|
using namespace XCEngine::Core;
|
||
|
|
|
||
|
|
FileWriter writer("output.txt");
|
||
|
|
if (writer.IsOpen()) {
|
||
|
|
// 写入原始字符数组
|
||
|
|
writer.Write("Hello, World!", 13);
|
||
|
|
writer.Write("\n", 1);
|
||
|
|
|
||
|
|
// 写入 String
|
||
|
|
Containers::String message = "This is a test string";
|
||
|
|
writer.Write(message);
|
||
|
|
|
||
|
|
writer.Flush();
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## 相关文档
|
||
|
|
|
||
|
|
- [FileWriter 总览](filewriter.md) - 返回类总览
|
||
|
|
- [Flush](Flush.md) - 刷新缓冲区
|