fwrite
注意
这个$函数以小写字母开头。
描述
向指定文件写入文本内容。
名称 | 说明 |
---|---|
File:handle | 要操作的文件句柄(由 fopen 返回)。 |
const string[] | 需要写入的文本字符串。 |
返回值
返回成功写入的字符串长度(整数值)。
示例
// 以"只写"模式打开"file.txt"
new File:handle = fopen("file.txt", io_write);
// 检查文件是否成功打开
if (handle)
{
// 操作成功
// 写入内容到文件
fwrite(handle, "I just wrote here!");
// 关闭文件
fclose(handle);
}
else
{
// 操作失败
print("无法打开\"file.txt\"文件。");
}
// 以"读写"模式打开"file.txt"
new File:handle = fopen("file.txt");
// 初始化缓冲区
new buf[128];
// 检查文件是否成功打开
if (handle)
{
// 读取文件全部内容
while(fread(handle, buf))
{
print(buf);
}
// 重置文件指针到起始位置
fseek(handle, _, seek_begin);
// 写入新内容
fwrite(handle, "I just wrote here!");
// 关闭文件
fclose(handle);
}
else
{
// 操作失败
print("文件\"file.txt\"不存在或无法访问。");
}
// 以"追加"模式打开"file.txt"
new File:handle = fopen("file.txt", io_append);
// 检查文件是否成功打开
if (handle)
{
// 追加写入内容(包含换行符)
fwrite(handle, "This is a test.\r\n");
// 关闭文件
fclose(handle);
}
else
{
// 操作失败
print("无法打开\"file.txt\"文件。");
}
注意事项
提示
本函数使用 UTF-8 编码写入文件,可能无法支持某些特殊语言符号。
相关函数
- fopen: 打开文件
- fclose: 关闭文件
- ftemp: 创建临时文件流
- fremove: 删除文件
- fread: 读取文件
- fputchar: 写入单个字符
- fgetchar: 读取单个字符
- fblockwrite: 写入数据块
- fblockread: 读取数据块
- fseek: 定位文件指针
- flength: 获取文件长度
- fexist: 检查文件是否存在
- fmatch: 匹配文件名模式
- ftell: 获取当前指针位置
- fflush: 刷新文件缓冲区
- fstat: 获取文件状态信息
- frename: 重命名文件
- fcopy: 复制文件
- filecrc: 计算 CRC32 校验值
- diskfree: 获取磁盘剩余空间
- fattrib: 设置文件属性
- fcreatedir: 创建目录