tpFile类
Utils
2025-07-05
介绍
文件操作类,提供文件处理和I/O功能
基础信息
头文件: | include <tpFile.h> |
---|---|
Cmake: | None |
发布版本: | V0.1.0 |
继承类: | |
派生类: |
Public 类型
enum OpenModeFlag {
NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002,
ReadWrite = ReadOnly | WriteOnly, Append = 0x0004, ReadWriteAppend
}
Public 成员函数
tpFile() | 默认构造函数 - 创建未初始化的文件对象 | |
---|---|---|
tpFile(const tpString &_fileName) | 构造函数 - 使用指定文件名初始化 | |
~tpFile() | 析构函数 - 自动关闭已打开的文件 | |
tpString | fileName() const | 获取当前关联的文件名 |
setFileName(const tpString &name) | 设置新的文件名 | |
tpFileInfo | fileInfo() | 获取文件的详细信息 |
exists() const | 检查当前关联的文件是否存在 | |
remove() | 删除当前关联的文件 | |
rename(const tpString &newName) | 重命名当前关联的文件 | |
copy(const tpString &newName) | 创建当前文件的副本 | |
size() const | 获取文件大小 | |
open(OpenModeFlag mode) | 以指定模式打开文件 | |
isOpen() const | 检查文件是否已打开 | |
isReadable() const | 检查文件是否可读(以读模式打开) | |
isWritable() const | 检查文件是否可写(以写模式打开) | |
close() | 关闭当前打开的文件 | |
pos() const | 获取当前读写位置 | |
seek(uint64_t offset) | 设置文件读写位置 | |
atEnd() const | 检查是否到达文件末尾 | |
flush() | 刷新写缓冲区到磁盘 | |
read(char *data, uint64_t maxlen) | 从文件读取数据到缓冲区 | |
tpString | read(uint64_t maxlen) | 读取最多指定字节数作为字符串 |
tpString | readAll() | 读取文件全部内容 |
readLine(char *data, uint64_t maxlen) | 读取一行数据到缓冲区 | |
tpString | readLine(uint64_t maxlen=0) | 读取一行数据作为字符串 |
write(const char *data, uint64_t len) | 写入二进制数据到文件 | |
write(const char *data) | 写入C字符串到文件 | |
write(const tpString &data) | 写入tpString数据到文件 | |
exists(const tpString &fileName) | 检查文件系统中是否存在指定文件 | |
remove(const tpString &fileName) | 删除指定文件 | |
rename(const tpString &oldName, const tpString &newName) | 重命名/移动文件到新位置 | |
copy(const tpString &fileName, const tpString &newName) | 复制文件到新位置 |
成员枚举类型说明
enum tpFile::OpenModeFlag
名称 | 值 | 描述 |
---|---|---|
NotOpen | 0x0000 | 文件未打开 |
ReadOnly | 0x0001 | 以只读模式打开文件 |
WriteOnly | 0x0002 | 以只写模式打开文件 |
ReadWrite | ReadOnly | WriteOnly |
Append | 0x0004 | 以追加模式打开文件(写入位置在文件末尾) |
ReadWriteAppend | 0x0008 |
成员函数说明
tpFile::tpFile()
点击查看...
默认构造函数 - 创建未初始化的文件对象
tpFile::tpFile(const tpString &_fileName)
点击查看...
构造函数 - 使用指定文件名初始化
tpFile::~tpFile()
点击查看...
析构函数 - 自动关闭已打开的文件
tpString tpFile::fileName() const
点击查看...
获取当前关联的文件名
返回值:
当前文件名的tpString对象
void tpFile::setFileName(const tpString &name)
点击查看...
设置新的文件名
tpFileInfo tpFile::fileInfo()
点击查看...
获取文件的详细信息
返回值:
包含文件元数据的tpFileInfo对象
bool tpFile::exists() const
点击查看...
检查当前关联的文件是否存在
返回值:
存在返回true,否则返回false
bool tpFile::remove()
点击查看...
删除当前关联的文件
返回值:
成功删除返回true,否则返回false
bool tpFile::rename(const tpString &newName)
点击查看...
重命名当前关联的文件
返回值:
重命名成功返回true,否则返回false
bool tpFile::copy(const tpString &newName)
点击查看...
创建当前文件的副本
返回值:
复制成功返回true,否则返回false
uint64_t tpFile::size() const
点击查看...
获取文件大小
返回值:
文件大小(字节数),不可用则返回0
bool tpFile::open(OpenModeFlag mode)
点击查看...
以指定模式打开文件
返回值:
成功打开返回true,否则返回false
bool tpFile::isOpen() const
点击查看...
检查文件是否已打开
返回值:
已打开返回true,否则返回false
bool tpFile::isReadable() const
点击查看...
检查文件是否可读(以读模式打开)
返回值:
可读返回true,否则返回false
bool tpFile::isWritable() const
点击查看...
检查文件是否可写(以写模式打开)
返回值:
可写返回true,否则返回false
void tpFile::close()
点击查看...
关闭当前打开的文件
uint64_t tpFile::pos() const
点击查看...
获取当前读写位置
返回值:
当前文件位置(字节偏移量)
bool tpFile::seek(uint64_t offset)
点击查看...
设置文件读写位置
返回值:
定位成功返回true,否则返回false
bool tpFile::atEnd() const
点击查看...
检查是否到达文件末尾
返回值:
到达文件尾返回true,否则返回false
bool tpFile::flush()
点击查看...
刷新写缓冲区到磁盘
返回值:
刷新成功返回true,否则返回false
uint64_t tpFile::read(char *data, uint64_t maxlen)
点击查看...
从文件读取数据到缓冲区
返回值:
实际读取的字节数
tpString tpFile::read(uint64_t maxlen)
点击查看...
读取最多指定字节数作为字符串
返回值:
包含读取数据的tpString对象
tpString tpFile::readAll()
点击查看...
读取文件全部内容
返回值:
包含文件完整内容的tpString对象
uint64_t tpFile::readLine(char *data, uint64_t maxlen)
点击查看...
读取一行数据到缓冲区
返回值:
实际读取的字节数(包含行终止符)
tpString tpFile::readLine(uint64_t maxlen=0)
点击查看...
读取一行数据作为字符串
返回值:
包含行数据的tpString对象
uint64_t tpFile::write(const char *data, uint64_t len)
点击查看...
写入二进制数据到文件
返回值:
实际写入的字节数
uint64_t tpFile::write(const char *data)
点击查看...
写入C字符串到文件
返回值:
实际写入的字节数(不包含终止符)
uint64_t tpFile::write(const tpString &data)
点击查看...
写入tpString数据到文件
返回值:
实际写入的字节数
static bool tpFile::exists(const tpString &fileName)
点击查看...
检查文件系统中是否存在指定文件
返回值:
存在返回true,否则返回false
static bool tpFile::remove(const tpString &fileName)
点击查看...
删除指定文件
返回值:
成功删除返回true,否则返回false
static bool tpFile::rename(const tpString &oldName, const tpString &newName)
点击查看...
重命名/移动文件到新位置
返回值:
重命名成功返回true,否则返回false
static bool tpFile::copy(const tpString &fileName, const tpString &newName)
点击查看...
复制文件到新位置
返回值:
复制成功返回true,否则返回false