QFileDevice class provides an interface for reading from and writing to open files. 更多...
头: | #include <QFileDevice> |
qmake: | QT += core |
Since: | Qt 5.0 |
继承: | QIODevice |
继承者: |
注意: 此类的所有函数 可重入 .
enum | FileError { NoError, ReadError, WriteError, FatalError, ..., CopyError } |
enum | FileHandleFlag { AutoCloseHandle, DontCloseHandle } |
flags | FileHandleFlags |
enum | MemoryMapFlags { NoOptions, MapPrivateOption } |
enum | Permission { ReadOwner, WriteOwner, ExeOwner, ReadUser, ..., ExeOther } |
flags | Permissions |
~QFileDevice () | |
FileError | error () const |
virtual QString | fileName () const |
bool | flush () |
int | handle () const |
uchar * | map (qint64 offset , qint64 size , MemoryMapFlags flags = NoOptions) |
virtual Permissions | permissions () const |
virtual bool | resize (qint64 sz ) |
virtual bool | setPermissions (Permissions permissions ) |
bool | unmap (uchar * address ) |
void | unsetError () |
virtual bool | atEnd () const |
virtual void | close () |
virtual bool | isSequential () const |
virtual qint64 | pos () const |
virtual bool | seek (qint64 pos ) |
virtual qint64 | size () const |
virtual qint64 | readData (char * data , qint64 len ) |
virtual qint64 | readLineData (char * data , qint64 maxlen ) |
virtual qint64 | writeData (const char * data , qint64 len ) |
QFileDevice class provides an interface for reading from and writing to open files.
QFileDevice is the base class for I/O devices that can read and write text and binary files and resources . QFile offers the main functionality, QFileDevice serves as a base class for sharing functionality with other file devices such as QTemporaryFile ,通过提供所有文件操作,打开通过 QFile or QTemporaryFile .
另请参阅 QFile and QTemporaryFile .
此枚举描述可能的错误,错误返回通过 error () 函数。
常量 | 值 | 描述 |
---|---|---|
QFileDevice::NoError
|
0
|
没有出现错误。 |
QFileDevice::ReadError
|
1
|
发生错误当读取文件时。 |
QFileDevice::WriteError
|
2
|
An error occurred when writing to the file. |
QFileDevice::FatalError
|
3
|
发生致命错误。 |
QFileDevice::ResourceError
|
4
|
Out of resources (e.g., too many open files, out of memory, etc.) |
QFileDevice::OpenError
|
5
|
The file could not be opened. |
QFileDevice::AbortError
|
6
|
The operation was aborted. |
QFileDevice::TimeOutError
|
7
|
出现超时。 |
QFileDevice::UnspecifiedError
|
8
|
An unspecified error occurred. |
QFileDevice::RemoveError
|
9
|
The file could not be removed. |
QFileDevice::RenameError
|
10
|
The file could not be renamed. |
QFileDevice::PositionError
|
11
|
The position in the file could not be changed. |
QFileDevice::ResizeError
|
12
|
The file could not be resized. |
QFileDevice::PermissionsError
|
13
|
The file could not be accessed. |
QFileDevice::CopyError
|
14
|
The file could not be copied. |
This enum is used when opening a file to specify additional options which only apply to files and not to a generic QIODevice .
常量 | 值 | 描述 |
---|---|---|
QFileDevice::AutoCloseHandle
|
0x0001
|
The file handle passed into open () should be closed by close (), the default behavior is that close just flushes the file and the application is responsible for closing the file handle. When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it. |
QFileDevice::DontCloseHandle
|
0
|
If not explicitly closed, the underlying file handle is left open when the QFile object is destroyed. |
FileHandleFlags 类型是 typedef 对于 QFlags <FileHandleFlag>. It stores an OR combination of FileHandleFlag values.
This enum describes special options that may be used by the map () 函数。
常量 | 值 | 描述 |
---|---|---|
QFileDevice::NoOptions
|
0
|
没有选项。 |
QFileDevice::MapPrivateOption
|
0x0001
|
The mapped memory will be private, so any modifications will not be visible to other processes and will not be written to disk. Any such modifications will be lost when the memory is unmapped. It is unspecified whether modifications made to the file made after the mapping is created will be visible through the mapped memory. This enum value was introduced in Qt 5.4. |
该枚举在 Qt 4.4 引入或被修改。
This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.
常量 | 值 | 描述 |
---|---|---|
QFileDevice::ReadOwner
|
0x4000
|
The file is readable by the owner of the file. |
QFileDevice::WriteOwner
|
0x2000
|
The file is writable by the owner of the file. |
QFileDevice::ExeOwner
|
0x1000
|
The file is executable by the owner of the file. |
QFileDevice::ReadUser
|
0x0400
|
文件对于用户是可读的。 |
QFileDevice::WriteUser
|
0x0200
|
文件对于用户是可写的。 |
QFileDevice::ExeUser
|
0x0100
|
文件可由用户执行。 |
QFileDevice::ReadGroup
|
0x0040
|
文件可由组读取。 |
QFileDevice::WriteGroup
|
0x0020
|
The file is writable by the group. |
QFileDevice::ExeGroup
|
0x0010
|
The file is executable by the group. |
QFileDevice::ReadOther
|
0x0004
|
The file is readable by anyone. |
QFileDevice::WriteOther
|
0x0002
|
The file is writable by anyone. |
QFileDevice::ExeOther
|
0x0001
|
The file is executable by anyone. |
警告: Because of differences in the platforms supported by Qt, the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version.
注意: On NTFS file systems, ownership and permissions checking is disabled by default for performance reasons. To enable it, include the following line:
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
Permission checking is then turned on and off by incrementing and decrementing
qt_ntfs_permission_lookup
by 1.
qt_ntfs_permission_lookup++; // turn checking on qt_ntfs_permission_lookup--; // turn it off again
The Permissions type is a typedef for QFlags <Permission>. It stores an OR combination of Permission values.
销毁文件设备,关闭它若有必要。
[virtual]
bool
QFileDevice::
atEnd
() const
重实现自 QIODevice::atEnd ().
返回
true
若到达 EOF (文件末尾);否则返回 false。
对于 Unix 中的常规空文件 (如那些在
/proc
),此函数返回
true
, since the file system reports that the size of such a file is 0. Therefore, you should not depend on atEnd() when reading data from such a file, but rather call
read
() until no more data can be read.
[virtual]
void
QFileDevice::
close
()
重实现自 QIODevice::close ().
调用 QFileDevice::flush () and closes the file. Errors from flush are ignored.
另请参阅 QIODevice::close ().
返回文件错误状态。
I/O 设备状态返回错误代码。例如,若
open
() 返回
false
, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
另请参阅 unsetError ().
[virtual]
QString
QFileDevice::
fileName
() const
Returns the name of the file. The default implementation in QFileDevice 返回 null 字符串。
将任何缓冲数据刷新到文件。返回
true
若成功;否则返回
false
.
返回文件的文件句柄。
这是小正整数,适合用于 C 库函数,譬如
fdopen()
and
fcntl()
. On systems that use file descriptors for sockets (i.e. Unix systems, but not Windows) the handle can be used with
QSocketNotifier
as well.
若文件未被打开,或存在错误,handle() 返回 -1。
另请参阅 QSocketNotifier .
[virtual]
bool
QFileDevice::
isSequential
() const
重实现自 QIODevice::isSequential ().
返回
true
若只能按顺序操纵文件;否则返回
false
.
大多数文件支持随机访问,但某些特殊文件不可以。
另请参阅 QIODevice::isSequential ().
映射 size bytes of the file into memory starting at offset . A file should be open for a map to succeed but the file does not need to stay open after the memory has been mapped. When the QFile is destroyed or a new file is opened with this object, any maps that have not been unmapped will automatically be unmapped.
The mapping will have the same open mode as the file (read and/or write), except when using MapPrivateOption , in which case it is always possible to write to the mapped memory.
Any mapping options can be passed through flags .
Returns a pointer to the memory or 0 if there is an error.
另请参阅 unmap ().
[virtual]
Permissions
QFileDevice::
permissions
() const
Returns the complete OR-ed together combination of QFile::Permission for the file.
另请参阅 setPermissions ().
[virtual]
qint64
QFileDevice::
pos
() const
重实现自 QIODevice::pos ().
[virtual protected]
qint64
QFileDevice::
readData
(
char
*
data
,
qint64
len
)
重实现自 QIODevice::readData ().
[virtual protected]
qint64
QFileDevice::
readLineData
(
char
*
data
,
qint64
maxlen
)
重实现自 QIODevice::readLineData ().
[virtual]
bool
QFileDevice::
resize
(
qint64
sz
)
设置文件大小 (以字节为单位)
sz
。返回
true
if the resize succeeds; false otherwise. If
sz
is larger than the file currently is, the new bytes will be set to 0; if
sz
is smaller, the file is simply truncated.
警告: 此函数可能失败,若文件不存在。
另请参阅 size ().
[virtual]
bool
QFileDevice::
seek
(
qint64
pos
)
重实现自 QIODevice::seek ().
对于随机访问设备,此函数将当前位置设为 pos , returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.
Seeking beyond the end of a file: If the position is beyond the end of a file, then seek() will not immediately extend the file. If a write is performed at this position, then the file will be extended. The content of the file between the previous end of file and the newly written data is UNDEFINED and varies between platforms and file systems.
[virtual]
bool
QFileDevice::
setPermissions
(
Permissions
permissions
)
将文件权限设为
permissions
指定。返回
true
若成功,或
false
若权限不能被修改。
警告: 此函数不操纵 ACL (访问控制列表),这可能限制其有效性。
另请参阅 permissions ().
[virtual]
qint64
QFileDevice::
size
() const
重实现自 QIODevice::size ().
返回文件大小。
对于 Unix 中的常规空文件 (如那些在
/proc
), this function returns 0; the contents of such a file are generated on demand in response to you calling
read
().
取消映射内存 address .
返回
true
若取消映射成功;否则 false。
另请参阅 map ().
把文件的错误设为 QFileDevice::NoError .
另请参阅 error ().
[virtual protected]
qint64
QFileDevice::
writeData
(const
char
*
data
,
qint64
len
)
重实现自 QIODevice::writeData ().