NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
file类 参考

文件操作类 更多...

#include <file.hpp>

class  line_iterator
 行迭代器 更多...
struct  chunk_info
 文件块信息 更多...

Public 类型

using size_type = size_t
 大小类型
using difference_type = ::off_t
 偏移量类型
using native_handle_type = int
 原生文件句柄类型

Public 成员函数

 file ()
 默认构造函数
 file (path pth, bool append=false, file_access access=file_access::READ_WRITE, file_shared share_mode=file_shared::SHARE_READ, file_creation creation=file_creation::OPEN_EXIST, file_attri attributes=file_attri::NORMAL)
 构造函数(打开文件)
 file (file &&other) noexcept
 移动构造函数
fileoperator= (file &&other) noexcept
 移动赋值运算符
 ~file ()
 析构函数
bool open (path pth, bool append=false, file_access access=file_access::READ_WRITE, file_shared share_mode=file_shared::SHARE_READ_WRITE, file_creation creation=file_creation::OPEN_EXIST, file_attri attributes=file_attri::NORMAL)
 打开文件
bool open (bool append=false, file_access access=file_access::READ_WRITE, file_shared share_mode=file_shared::SHARE_READ_WRITE, file_creation creation=file_creation::OPEN_EXIST, file_attri attributes=file_attri::NORMAL)
 重新打开文件(使用原有路径)
void close () noexcept
 关闭文件
bool flush () noexcept
 刷新缓冲区
size_type write (const string &data, size_type size)
 写入数据
size_type write (const string &data)
 写入字符串
size_type write (const void *data, size_type size)
 写入二进制数据
size_type read (void *buffer, size_type size) const
 读取数据到缓冲区
size_type read (string &out, size_type size) const
 读取数据到字符串
size_type read (string &out) const
 读取数据到字符串(自动调整大小)
NEFORCE_NODISCARD string read () const
 读取整个文件
size_type read_binary (void *out, size_type size) const
 读取二进制数据到缓冲区
size_type read_binary (string &out, size_type size) const
 读取二进制数据到字符串
size_type read_binary (string &out) const
 读取二进制数据到字符串(自动调整大小)
NEFORCE_NODISCARD string read_binary () const
 读取整个文件(二进制模式)
bool read_line (string &line) const
 读取一行
NEFORCE_NODISCARD string read_line () const
 读取一行
NEFORCE_NODISCARD vector< stringread_lines () const
 读取所有行
vector< stringread_chunks (size_type chunk_size=buffer_size *16) const
 读取文件块
bool write_chunks (const vector< string > &chunks)
 写入文件块
NEFORCE_NODISCARD vector< chunk_infochunks_info (size_type chunk_size) const
 获取块信息
bool seek (difference_type distance, file_pointer method=file_pointer::END) const
 移动文件指针
difference_type tell () const
 获取当前文件指针位置
difference_type system_tell () const
 获取系统文件指针位置
bool prefetch (size_type hint_size=0) const
 预取数据到缓存
bool truncate (difference_type size) const
 截断文件
NEFORCE_NODISCARD size_type size () const
 获取文件大小
bool size (size_type &out_size) const
 获取文件大小
NEFORCE_NODISCARD uint64_t size64 () const
 获取文件大小
NEFORCE_NODISCARD native_handle_type native_handle () const noexcept
 获取原生文件句柄
NEFORCE_NODISCARD const pathfile_path () const noexcept
 获取文件路径
NEFORCE_NODISCARD bool is_opened () const noexcept
 检查文件是否已打开
NEFORCE_NODISCARD bool is_append () const noexcept
 检查是否为追加模式
NEFORCE_NODISCARD const error_code & last_error_code () const noexcept
 获取最后错误码
void clear_error () noexcept
 清除错误状态
NEFORCE_NODISCARD line_iterator begin_lines () const
 获取行迭代器起始
NEFORCE_NODISCARD line_iterator end_lines () const
 获取行迭代器结束
NEFORCE_NODISCARD file_mappermapper () noexcept
 获取内存映射对象
NEFORCE_NODISCARD file_lockerlocker () noexcept
 获取文件锁对象
NEFORCE_NODISCARD file_infoinfo () noexcept
 获取文件信息对象
NEFORCE_NODISCARD file_asyncasync () noexcept
 获取异步I/O对象

静态 Public 属性

static constexpr size_t buffer_size = 8192
 文件操作缓冲区大小

详细描述

文件操作类

支持移动语义,不支持拷贝,线程安全

主要特性:

  • 同步/异步IO操作
  • 内存映射
  • 区域锁定
  • 属性管理
  • 比较和差异分析
  • 自动缓冲和预取优化

线程安全,支持移动语义,不支持拷贝。

在文件 file.hpp43 行定义.

构造及析构函数说明

◆ file() [1/3]

file::file ( )

默认构造函数

创建未打开的文件对象。

引用了 file().

被这些函数引用 file(), file(), file(), file::line_iterator::line_iterator(), operator=() , 以及 ~file().

◆ file() [2/3]

file::file ( path pth,
bool append = false,
file_access access = file_access::READ_WRITE,
file_shared share_mode = file_shared::SHARE_READ,
file_creation creation = file_creation::OPEN_EXIST,
file_attri attributes = file_attri::NORMAL )
explicit

构造函数(打开文件)

参数
pth文件路径
append是否为追加模式,默认为false
access访问模式,默认为读写
share_mode共享模式,默认为只读共享
creation创建方式,默认为打开已存在文件
attributes文件属性,默认为普通

创建并打开文件。

引用了 file().

◆ file() [3/3]

file::file ( file && other)
noexcept

移动构造函数

参数
other要移动的文件对象

引用了 file().

◆ ~file()

file::~file ( )

析构函数

自动关闭文件,刷新缓冲区。

引用了 file().

成员函数说明

◆ async()

NEFORCE_NODISCARD file_async & file::async ( )
inlinenoexcept

获取异步I/O对象

返回
异步I/O对象引用

在文件 file.hpp523 行定义.

◆ begin_lines()

NEFORCE_NODISCARD line_iterator file::begin_lines ( ) const
inline

获取行迭代器起始

返回
行迭代器

在文件 file.hpp490 行定义.

引用了 begin_lines().

被这些函数引用 begin_lines().

◆ chunks_info()

NEFORCE_NODISCARD vector< chunk_info > file::chunks_info ( size_type chunk_size) const

获取块信息

参数
chunk_size块大小
返回
块信息向量

计算文件分块的信息,不实际读取数据。

引用了 chunks_info().

被这些函数引用 chunks_info().

◆ close()

void file::close ( )
noexcept

关闭文件

刷新缓冲区,释放所有资源。

引用了 close().

被这些函数引用 close().

◆ end_lines()

NEFORCE_NODISCARD line_iterator file::end_lines ( ) const
inline

获取行迭代器结束

返回
结束迭代器

在文件 file.hpp496 行定义.

◆ file_path()

NEFORCE_NODISCARD const path & file::file_path ( ) const
inlinenoexcept

获取文件路径

返回
路径引用

在文件 file.hpp461 行定义.

◆ flush()

bool file::flush ( )
noexcept

刷新缓冲区

返回
成功返回true

将写缓冲区内容写入磁盘,同时刷新操作系统缓存。

引用了 flush().

被这些函数引用 flush().

◆ info()

NEFORCE_NODISCARD file_info & file::info ( )
inlinenoexcept

获取文件信息对象

返回
文件信息对象引用

在文件 file.hpp516 行定义.

◆ is_append()

NEFORCE_NODISCARD bool file::is_append ( ) const
inlinenoexcept

检查是否为追加模式

返回
是否为追加模式

在文件 file.hpp473 行定义.

◆ is_opened()

NEFORCE_NODISCARD bool file::is_opened ( ) const
inlinenoexcept

检查文件是否已打开

返回
是否已打开

在文件 file.hpp467 行定义.

◆ last_error_code()

NEFORCE_NODISCARD const error_code & file::last_error_code ( ) const
inlinenoexcept

获取最后错误码

返回
错误码

在文件 file.hpp479 行定义.

◆ locker()

NEFORCE_NODISCARD file_locker & file::locker ( )
inlinenoexcept

获取文件锁对象

返回
文件锁对象引用

在文件 file.hpp509 行定义.

◆ mapper()

NEFORCE_NODISCARD file_mapper & file::mapper ( )
inlinenoexcept

获取内存映射对象

返回
内存映射对象引用

在文件 file.hpp502 行定义.

◆ native_handle()

NEFORCE_NODISCARD native_handle_type file::native_handle ( ) const
inlinenoexcept

获取原生文件句柄

返回
文件句柄

在文件 file.hpp455 行定义.

引用了 native_handle().

被这些函数引用 native_handle().

◆ open() [1/2]

bool file::open ( bool append = false,
file_access access = file_access::READ_WRITE,
file_shared share_mode = file_shared::SHARE_READ_WRITE,
file_creation creation = file_creation::OPEN_EXIST,
file_attri attributes = file_attri::NORMAL )

重新打开文件(使用原有路径)

参数
append是否为追加模式
access访问模式
share_mode共享模式
creation创建方式
attributes文件属性
返回
打开成功返回true

引用了 open().

◆ open() [2/2]

bool file::open ( path pth,
bool append = false,
file_access access = file_access::READ_WRITE,
file_shared share_mode = file_shared::SHARE_READ_WRITE,
file_creation creation = file_creation::OPEN_EXIST,
file_attri attributes = file_attri::NORMAL )

打开文件

参数
pth文件路径
append是否为追加模式
access访问模式
share_mode共享模式
creation创建方式
attributes文件属性
返回
打开成功返回true

引用了 open().

被这些函数引用 open() , 以及 open().

◆ operator=()

file & file::operator= ( file && other)
noexcept

移动赋值运算符

参数
other要移动的文件对象
返回
自身引用

引用了 file().

◆ prefetch()

bool file::prefetch ( size_type hint_size = 0) const

预取数据到缓存

参数
hint_size提示预取大小(字节)
返回
成功返回true

建议操作系统将后续数据预加载到内存,提高顺序访问性能。

引用了 prefetch().

被这些函数引用 prefetch().

◆ read() [1/4]

NEFORCE_NODISCARD string file::read ( ) const

读取整个文件

返回
文件内容字符串

引用了 read().

◆ read() [2/4]

size_type file::read ( string & out) const

读取数据到字符串(自动调整大小)

参数
out输出字符串
返回
实际读取的字节数

引用了 read().

◆ read() [3/4]

size_type file::read ( string & out,
size_type size ) const

读取数据到字符串

参数
out输出字符串
size要读取的大小
返回
实际读取的字节数

引用了 read() , 以及 size().

◆ read() [4/4]

size_type file::read ( void * buffer,
size_type size ) const

读取数据到缓冲区

参数
buffer缓冲区指针
size要读取的大小
返回
实际读取的字节数

引用了 read() , 以及 size().

被这些函数引用 read(), read(), read() , 以及 read().

◆ read_binary() [1/4]

NEFORCE_NODISCARD string file::read_binary ( ) const

读取整个文件(二进制模式)

返回
文件内容字符串

引用了 read_binary().

◆ read_binary() [2/4]

size_type file::read_binary ( string & out) const

读取二进制数据到字符串(自动调整大小)

参数
out输出字符串
返回
实际读取的字节数

引用了 read_binary().

◆ read_binary() [3/4]

size_type file::read_binary ( string & out,
size_type size ) const

读取二进制数据到字符串

参数
out输出字符串
size要读取的大小
返回
实际读取的字节数

引用了 read_binary() , 以及 size().

◆ read_binary() [4/4]

size_type file::read_binary ( void * out,
size_type size ) const

读取二进制数据到缓冲区

参数
out缓冲区指针
size要读取的大小
返回
实际读取的字节数

引用了 read_binary() , 以及 size().

被这些函数引用 read_binary(), read_binary(), read_binary() , 以及 read_binary().

◆ read_chunks()

vector< string > file::read_chunks ( size_type chunk_size = buffer_size *16) const

读取文件块

参数
chunk_size块大小(字节)
返回
块字符串向量

将文件按固定大小分块,适用于流式处理大文件。

引用了 buffer_size , 以及 read_chunks().

被这些函数引用 read_chunks().

◆ read_line() [1/2]

NEFORCE_NODISCARD string file::read_line ( ) const

读取一行

返回
行字符串,文件末尾返回空字符串

引用了 read_line().

◆ read_line() [2/2]

bool file::read_line ( string & line) const

读取一行

参数
line输出行
返回
是否成功读取(包括空行)

引用了 read_line().

被这些函数引用 read_line() , 以及 read_line().

◆ read_lines()

NEFORCE_NODISCARD vector< string > file::read_lines ( ) const

读取所有行

返回
行字符串向量

将整个文件按行分割,适用于中等大小文件。

引用了 read_lines().

被这些函数引用 read_lines().

◆ seek()

bool file::seek ( difference_type distance,
file_pointer method = file_pointer::END ) const

移动文件指针

参数
distance移动距离(字节)
method移动方式
返回
移动成功返回true

追加模式下只能seek到文件末尾。

引用了 distance() , 以及 seek().

被这些函数引用 seek().

◆ size() [1/2]

NEFORCE_NODISCARD size_type file::size ( ) const

获取文件大小

返回
文件大小(字节),超过4GB返回0

引用了 size().

被这些函数引用 read(), read(), read_binary(), read_binary(), size(), size(), truncate(), write() , 以及 write().

◆ size() [2/2]

bool file::size ( size_type & out_size) const

获取文件大小

参数
out_size输出文件大小(字节)
返回
成功返回true

引用了 size().

◆ size64()

NEFORCE_NODISCARD uint64_t file::size64 ( ) const

获取文件大小

返回
文件大小(字节),支持大文件

引用了 size64().

被这些函数引用 size64().

◆ system_tell()

difference_type file::system_tell ( ) const

获取系统文件指针位置

返回
系统当前位置(字节),-1表示错误

返回操作系统层面的文件指针位置,不考虑缓冲区。

引用了 system_tell().

被这些函数引用 system_tell().

◆ tell()

difference_type file::tell ( ) const

获取当前文件指针位置

返回
当前位置(字节),-1表示错误

返回逻辑文件指针位置(考虑缓冲区)。

引用了 tell().

被这些函数引用 tell().

◆ truncate()

bool file::truncate ( difference_type size) const

截断文件

参数
size新文件大小(字节)
返回
成功返回true

将文件截断到指定大小,多余数据丢失。 如果新大小大于原文件,文件会扩展(内容未定义)。

引用了 size() , 以及 truncate().

被这些函数引用 truncate().

◆ write() [1/3]

size_type file::write ( const string & data)

写入字符串

参数
data要写入的字符串
返回
实际写入的字节数

引用了 data() , 以及 write().

◆ write() [2/3]

size_type file::write ( const string & data,
size_type size )

写入数据

参数
data要写入的数据
size要写入的大小
返回
实际写入的字节数

引用了 data(), size() , 以及 write().

被这些函数引用 write(), write() , 以及 write().

◆ write() [3/3]

size_type file::write ( const void * data,
size_type size )

写入二进制数据

参数
data数据指针
size数据大小
返回
实际写入的字节数

引用了 data(), size() , 以及 write().

◆ write_chunks()

bool file::write_chunks ( const vector< string > & chunks)

写入文件块

参数
chunks块向量
返回
写入成功返回true

引用了 write_chunks().

被这些函数引用 write_chunks().

类成员变量说明

◆ buffer_size

size_t file::buffer_size = 8192
staticconstexpr

文件操作缓冲区大小

默认的I/O缓冲区大小。其会根据文件大小自动调整。

在文件 file.hpp50 行定义.

被这些函数引用 read_chunks().


该类的文档由以下文件生成: