QFile 类

The QFile class provides an interface for reading from and writing to files. 更多...

头: #include <QFile>
继承: QIODevice
继承者: QTemporaryFile

注意: 此类的所有函数 可重入 , except for setEncodingFunction () 和 setDecodingFunction (), which are nonreentrant.

公共类型

typedef DecoderFn
typedef EncoderFn
enum FileError { NoError, ReadError, WriteError, FatalError, ..., CopyError }
enum FileHandleFlag { AutoCloseHandle, DontCloseHandle }
flags FileHandleFlags
enum MemoryMapFlags { NoOptions }
enum Permission { ReadOwner, WriteOwner, ExeOwner, ReadUser, ..., ExeOther }
typedef PermissionSpec
flags Permissions

公共函数

QFile (const QString & name )
QFile (QObject * parent )
QFile (const QString & name , QObject * parent )
~QFile ()
bool copy (const QString & newName )
FileError error () const
bool exists () const
QString fileName () const
bool flush ()
int handle () const
bool link (const QString & linkName )
uchar * map (qint64 offset , qint64 size , MemoryMapFlags flags = NoOptions)
bool open (FILE * fh , OpenMode mode )
bool open (FILE * fh , OpenMode mode , FileHandleFlags handleFlags )
bool open (int fd , OpenMode mode )
bool open (int fd , OpenMode mode , FileHandleFlags handleFlags )
bool open (const RFile & f , OpenMode mode , FileHandleFlags handleFlags = DontCloseHandle)
权限 permissions () const
bool remove ()
bool rename (const QString & newName )
bool resize (qint64 sz )
void setFileName (const QString & name )
bool setPermissions (Permissions permissions )
QString symLinkTarget () const
bool unmap (uchar * address )
void unsetError ()

重实现公共函数

virtual bool atEnd () const
virtual void close ()
virtual bool isSequential () const
virtual bool open (OpenMode mode )
virtual qint64 pos () const
virtual bool seek (qint64 pos )
virtual qint64 size () const

静态公共成员

bool copy (const QString & fileName , const QString & newName )
QString decodeName (const QByteArray & localFileName )
QString decodeName (const char * localFileName )
QByteArray encodeName (const QString & fileName )
bool exists (const QString & fileName )
bool link (const QString & fileName , const QString & linkName )
权限 permissions (const QString & fileName )
bool remove (const QString & fileName )
bool rename (const QString & oldName , const QString & newName )
bool resize (const QString & fileName , qint64 sz )
void setDecodingFunction (DecoderFn function )
void setEncodingFunction (EncoderFn function )
bool setPermissions (const QString & fileName , Permissions permissions )
QString symLinkTarget (const QString & fileName )

重实现保护函数

virtual qint64 readData (char * data , qint64 len )
virtual qint64 readLineData (char * data , qint64 maxlen )
virtual qint64 writeData (const char * data , qint64 len )

额外继承成员

详细描述

The QFile class provides an interface for reading from and writing to files.

QFile is an I/O device for reading and writing text and binary files and resources QFile 可以单独使用,或更方便一起使用与 QTextStream or QDataStream .

通常在构造函数中传递文件名,但可以随时设置它使用 setFileName (). QFile expects the file separator to be '/' regardless of operating system. The use of other separators (e.g., '\') is not supported.

可以检查文件是否存在使用 exists (),和移除文件使用 remove ()。(更高级的文件系统相关操作的提供由 QFileInfo and QDir )。

打开文件采用 open (),关闭采用 close (),和刷新采用 flush ()。数据的读写通常是使用 QDataStream or QTextStream ,但也可以调用 QIODevice 继承函数 read (), readLine (), readAll (), write (). QFile 还继承 getChar (), putChar (),和 ungetChar (),每次操控一字符。

文件大小的返回是通过 size ()。可以获取当前文件位置使用 pos (),或移至新文件位置使用 seek ()。若已到达 EOF (文件末尾), atEnd () returns true.

直接读取文件

以下范例逐行读取文本文件:

    QFile file("in.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        process_line(line);
    }
					

The QIODevice::Text flag passed to open () tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.

使用流读取文件

下一范例使用 QTextStream 以逐行读取文本文件:

    QFile file("in.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        process_line(line);
    }
					

QTextStream 负责将存储在磁盘中的 8 位数据转换成 16 位 Unicode QString . By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; see QTextCodec::codecForLocale () for details). This can be changed using setCodec().

要写入文本,可以使用操作符 <<(),重载以接受 QTextStream 在左侧和各种数据类型 (包括 QString ) 在右侧:

    QFile file("out.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    QTextStream out(&file);
    out << "The magic number is: " << 49 << "\n";
					

QDataStream 类似,可以使用操作符 <<() 写入数据,和使用操作符 >>() 读回它。见类文档编制,了解细节。

When you use QFile , QFileInfo ,和 QDir to access the file system with Qt, you can use Unicode file names. On Unix, these file names are converted to an 8-bit encoding. If you want to use standard C++ APIs ( <cstdio> or <iostream> ) or platform-specific APIs to access files instead of QFile , you can use the encodeName () 和 decodeName () functions to convert between Unicode file names and 8-bit file names.

在 Unix,有一些特殊系统文件 (如在 /proc ) 其中 size () 将始终返回 0,仍然可以从这种文件读取更多数据;直接生成数据是为响应调用 read ()。在此情况下,不管怎样,不可以使用 atEnd () 以确定是否有更多数据要读取 (由于 atEnd () 将返回 true 对于声明拥有大小 0 的文件)。相反,应该调用 readAll (),或调用 read () 或 readLine () 重复,直到无法读取更多数据。下一范例使用 QTextStream 以读取 /proc/modules 逐行:

    QFile file("/proc/modules");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    QTextStream in(&file);
    QString line = in.readLine();
    while (!line.isNull()) {
        process_line(line);
        line = in.readLine();
    }
					

信号

不像其它 QIODevice 实现,譬如 QTcpSocket , QFile does not emit the aboutToClose (), bytesWritten (),或 readyRead () signals. This implementation detail means that QFile is not suitable for reading and writing certain types of files, such as device files on Unix platforms.

平台具体问题

File permissions are handled differently on Linux/Mac OS X and Windows. In a non writable directory on Linux, files cannot be created. This is not always the case on Windows, where, for instance, the 'My Documents' directory usually is not writable, but it is still possible to create files in it.

另请参阅 QTextStream , QDataStream , QFileInfo , QDir ,和 Qt 资源系统 .

成员类型文档编制

typedef QFile:: DecoderFn

这是采用以下签名的函数指针的 typedef:

QString myDecoderFunc(const QByteArray &localFileName);
					

另请参阅 setDecodingFunction ().

typedef QFile:: EncoderFn

这是采用以下签名的函数指针的 typedef:

QByteArray myEncoderFunc(const QString &fileName);
					

另请参阅 setEncodingFunction () 和 encodeName ().

enum QFile:: FileError

此枚举描述可能的错误,错误返回通过 error () 函数。

常量 描述
QFile::NoError 0 没有出现错误。
QFile::ReadError 1 发生错误当读取文件时。
QFile::WriteError 2 出现错误,当写入文件时。
QFile::FatalError 3 出现致命错误。
QFile::ResourceError 4
QFile::OpenError 5 文件无法打开。
QFile::AbortError 6 操作被中止。
QFile::TimeOutError 7 出现超时。
QFile::UnspecifiedError 8 出现未指定错误。
QFile::RemoveError 9 文件无法删除。
QFile::RenameError 10 文件无法重命名。
QFile::PositionError 11 文件位置无法改变。
QFile::ResizeError 12 文件无法重置尺寸。
QFile::PermissionsError 13 文件无法访问。
QFile::CopyError 14 文件无法拷贝。

enum QFile:: FileHandleFlag
flags QFile:: FileHandleFlags

使用此枚举当打开文件时,以指定附加选项仅适用于文件而不适用于一般 QIODevice .

常量 描述
QFile::AutoCloseHandle 0x0001 文件句柄被传入 open () 应关闭通过 close (), the default behaviour 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.
QFile::DontCloseHandle 0 文件句柄被传入 open () will not be closed by Qt. The application must ensure that close () 被调用。

该枚举在 Qt 4.8 引入或被修改。

FileHandleFlags 类型是 typedef 对于 QFlags <FileHandleFlag>。它存储 FileHandleFlag 值的 OR 组合。

enum QFile:: MemoryMapFlags

此枚举描述的特殊选项可以用于 map () 函数。

常量 描述
QFile::NoOptions 0 没有选项。

该枚举在 Qt 4.4 引入或被修改。

enum QFile:: Permission
flags QFile:: Permissions

此枚举用于 permission() 函数,以报告文件的权限和所有权。可以把值 OR 在一起,以测试多权限和所有权值。

常量 描述
QFile::ReadOwner 0x4000 文件可由文件的所有者读取。
QFile::WriteOwner 0x2000 文件可由文件的所有者写入。
QFile::ExeOwner 0x1000 文件可由文件的所有者执行。
QFile::ReadUser 0x0400 文件对于用户是可读的。
QFile::WriteUser 0x0200 文件对于用户是可写的。
QFile::ExeUser 0x0100 文件可由用户执行。
QFile::ReadGroup 0x0040 文件可由组读取。
QFile::WriteGroup 0x0020 文件可由组写入。
QFile::ExeGroup 0x0010 文件可由组执行。
QFile::ReadOther 0x0004 The file is readable by anyone.
QFile::WriteOther 0x0002 The file is writable by anyone.
QFile::ExeOther 0x0001 The file is executable by anyone.

警告: 由于 Qt 支持的平台差异,ReadUser、WriteUser 及 ExeUser 的语义从属平台:Unix 返回文件所有者的权限,而 Windows 返回当前用户的权限。此行为在未来 Qt 版本中可能改变。

Note that Qt does not by default check for permissions on NTFS file systems, as this may decrease the performance of file handling considerably. It is possible to force permission checking on NTFS by including the following code in your source:

extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
					

然后打开和关闭权限校验,通过递增和递减 qt_ntfs_permission_lookup by 1.

qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again
					

Permissions 类型是 typedef 对于 QFlags <Permission>。它存储 Permission 值的 OR 组合。

typedef QFile:: PermissionSpec

使用 QFile::Permission 代替。

成员函数文档编制

QFile:: QFile (const QString & name )

构造新文件对象以表示文件采用给定 name .

QFile:: QFile ( QObject * parent )

构造新文件对象采用给定 parent .

QFile:: QFile (const QString & name , QObject * parent )

构造新文件对象采用给定 parent 表示文件采用指定 name .

QFile:: ~QFile ()

销毁文件对象,关闭它若有必要。

[虚拟] bool QFile:: atEnd () const

重实现自 QIODevice::atEnd ().

Returns true if the end of the file has been reached; otherwise returns false.

Unix 常规空文件 (如,那些在 /proc ), this function returns 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 () 直到无法读取更多数据。

[虚拟] void QFile:: close ()

重实现自 QIODevice::close ().

调用 QFile::flush () 并关闭文件。忽略 flush 错误。

另请参阅 QIODevice::close ().

bool QFile:: copy (const QString & newName )

拷贝目前指定的文件通过 fileName () 到文件称为 newName . Returns true if successful; otherwise returns false.

注意:若文件采用名称 newName already exists, copy() returns false (i.e. QFile 不会覆写它)。

关闭源文件在拷贝它之前。

另请参阅 setFileName ().

[static] bool QFile:: copy (const QString & fileName , const QString & newName )

这是重载函数。

拷贝文件 fileName to newName . Returns true if successful; otherwise returns false.

若文件采用名称 newName already exists, copy () returns false (i.e., QFile 不会覆写它)。

另请参阅 rename ().

[static] QString QFile:: decodeName (const QByteArray & localFileName )

这做反向 QFile::encodeName () 使用 localFileName .

另请参阅 setDecodingFunction () 和 encodeName ().

[static] QString QFile:: decodeName (const char * localFileName )

这是重载函数。

返回 Unicode 版本为给定 localFileName 。见 encodeName () 了解细节。

[static] QByteArray QFile:: encodeName (const QString & fileName )

By default, this function converts fileName to the local 8-bit encoding determined by the user's locale. This is sufficient for file names that the user chooses. File names hard-coded into the application should only use 7-bit ASCII filename characters.

另请参阅 decodeName () 和 setEncodingFunction ().

FileError QFile:: error () const

返回文件错误状态。

I/O 设备状态返回错误代码。例如,若 open () returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.

另请参阅 unsetError ().

[static] bool QFile:: exists (const QString & fileName )

Returns true if the file specified by fileName exists; otherwise returns false.

bool QFile:: exists () const

这是重载函数。

Returns true if the file specified by fileName () exists; otherwise returns false.

另请参阅 fileName () 和 setFileName ().

QString QFile:: fileName () const

返回名称设置通过 setFileName () 或到 QFile 构造函数。

另请参阅 setFileName () 和 QFileInfo::fileName ().

bool QFile:: flush ()

Flushes any buffered data to the file. Returns true if successful; otherwise returns false.

int QFile:: handle () const

返回文件的文件句柄。

This is a small positive integer, suitable for use with C library functions such as 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 还。

若文件未被打开,或存在错误,handle() 返回 -1。

This function is not supported on Windows CE.

On Symbian, this function returns -1 if the file was opened normally, as Symbian OS native file handles do not fit in an int, and are incompatible with C library functions that the handle would be used for. If the file was opened using the overloads that take an open C library file handle / file descriptor, then this function returns that same handle.

另请参阅 QSocketNotifier .

[虚拟] bool QFile:: isSequential () const

重实现自 QIODevice::isSequential ().

Returns true if the file can only be manipulated sequentially; otherwise returns false.

大多数文件支持随机访问,但某些特殊文件不可以。

另请参阅 QIODevice::isSequential ().

创建链接命名 linkName 指向文件目前指定通过 fileName (). What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.

此函数不会覆写文件系统中已存在的实体;在此情况下, link() 将返回 false 并设置 error() to return RenameError .

注意: 要在 Windows 创建有效链接, linkName 必须拥有 .lnk 文件扩展名。

注意: Symbian filesystem does not support links.

另请参阅 setFileName ().

这是重载函数。

创建链接命名 linkName 指向文件 fileName . What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.

另请参阅 link ().

uchar * QFile:: map ( qint64 offset , qint64 size , MemoryMapFlags flags = NoOptions)

映射 size 字节的文件到内存起始于 offset 。要成功映射,应打开文件。但文件不需要保持打开,在映射内存后。当 QFile 被销毁 (或采用此对象打开新文件),任何尚未取消映射的映射都将自动取消映射。

可以传递任何映射选项透过 flags .

Returns a pointer to the memory or 0 if there is an error.

注意: On Windows CE 5.0 the file will be closed before mapping occurs.

该函数在 Qt 4.4 引入。

另请参阅 unmap () 和 QAbstractFileEngine::supportsExtension ().

[虚拟] bool QFile:: open ( OpenMode mode )

重实现自 QIODevice::open ().

打开文件使用 OpenMode mode ,返回 true,若成功;否则返回 false。

The mode 必须为 QIODevice::ReadOnly , QIODevice::WriteOnly ,或 QIODevice::ReadWrite 。它还可以拥有额外标志,譬如 QIODevice::Text and QIODevice::Unbuffered .

注意: WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

另请参阅 QIODevice::OpenMode and setFileName ().

bool QFile:: open ( FILE * fh , OpenMode mode )

这是重载函数。

打开现有文件句柄 fh 以给定 mode . Returns true if successful; otherwise returns false.

范例:

#include <stdio.h>
void printError(const char* msg)
{
    QFile file;
    file.open(stderr, QIODevice::WriteOnly);
    file.write(msg, qstrlen(msg));        // write to stderr
    file.close();
}
					

QFile is opened using this function, close () 不会实际关闭文件,而仅刷新它。

警告:

  1. fh 不是指常规文件,如,它是 stdin , stdout ,或 stderr ,可能无法 seek (). size () 返回 0 在此情况下。见 QIODevice::isSequential () 了解更多信息。
  2. 由于此函数打开文件不用指定文件名,所以,无法使用此 QFile 采用 QFileInfo .

注意: For Windows CE you may not be able to call resize ().

Windows 平台注意事项

fh must be opened in binary mode (i.e., the mode string must contain 'b', as in "rb" or "wb") when accessing files and other random-access devices. Qt will translate the end-of-line characters if you pass QIODevice::Text to mode 。顺序设备 (譬如:stdin 和 stdout) 不受此局限性的影响。

需要启用对控制台应用程序的支持,为在控制台使用 stdin (标准输入)、stdout (标准输出) 及 stderr (标准错误) 流。要做到这,把以下声明添加到应用程序工程文件:

CONFIG += console
					

另请参阅 close () 和 qmake Variable Reference .

bool QFile:: open ( FILE * fh , OpenMode mode , FileHandleFlags handleFlags )

这是重载函数。

打开现有文件句柄 fh 以给定 mode . Returns true if successful; otherwise returns false.

范例:

#include <stdio.h>
void printError(const char* msg)
{
    QFile file;
    file.open(stderr, QIODevice::WriteOnly);
    file.write(msg, qstrlen(msg));        // write to stderr
    file.close();
}
					

QFile 是使用此函数打开的,行为对于 close () is controlled by the AutoCloseHandle flag. If AutoCloseHandle is specified, and this function succeeds, then calling close () 会关闭采纳句柄。否则, close () 不会实际关闭文件,而仅刷新它。

警告:

  1. fh 不是指常规文件,如,它是 stdin , stdout ,或 stderr ,可能无法 seek (). size () 返回 0 在此情况下。见 QIODevice::isSequential () 了解更多信息。
  2. 由于此函数打开文件不用指定文件名,所以,无法使用此 QFile 采用 QFileInfo .

注意: For Windows CE you may not be able to call resize ().

Windows 平台注意事项

fh must be opened in binary mode (i.e., the mode string must contain 'b', as in "rb" or "wb") when accessing files and other random-access devices. Qt will translate the end-of-line characters if you pass QIODevice::Text to mode 。顺序设备 (譬如:stdin 和 stdout) 不受此局限性的影响。

需要启用对控制台应用程序的支持,为在控制台使用 stdin (标准输入)、stdout (标准输出) 及 stderr (标准错误) 流。要做到这,把以下声明添加到应用程序工程文件:

CONFIG += console
					

另请参阅 close () 和 qmake Variable Reference .

bool QFile:: open ( int fd , OpenMode mode )

这是重载函数。

打开现有文件描述符 fd 以给定 mode . Returns true if successful; otherwise returns false.

QFile is opened using this function, close () does not actually close the file.

The QFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.

警告: fd 不是常规文件,如为 0 ( stdin ), 1 ( stdout ),或 2 ( stderr ),可能无法 seek ()。在此情况下, size () 返回 0 。见 QIODevice::isSequential () 了解更多信息。

警告: For Windows CE you may not be able to call seek (), setSize(), fileTime(). size () 返回 0 .

警告: 由于此函数打开文件不用指定文件名,所以,无法使用此 QFile 采用 QFileInfo .

另请参阅 close ().

bool QFile:: open ( int fd , OpenMode mode , FileHandleFlags handleFlags )

这是重载函数。

打开现有文件描述符 fd 以给定 mode . Returns true if successful; otherwise returns false.

QFile 是使用此函数打开的,行为对于 close () is controlled by the handleFlags argument. If AutoCloseHandle is specified, and this function succeeds, then calling close () 会关闭采纳句柄。否则, close () 不会实际关闭文件,而仅刷新它。

The QFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.

警告: fd 不是常规文件,如为 0 ( stdin ), 1 ( stdout ),或 2 ( stderr ),可能无法 seek ()。在此情况下, size () 返回 0 。见 QIODevice::isSequential () 了解更多信息。

警告: For Windows CE you may not be able to call seek (), setSize(), fileTime(). size () 返回 0 .

警告: 由于此函数打开文件不用指定文件名,所以,无法使用此 QFile 采用 QFileInfo .

另请参阅 close ().

bool QFile:: open (const RFile & f , OpenMode mode , FileHandleFlags handleFlags = DontCloseHandle)

这是重载函数。

Opens the existing file object f 以给定 mode . Returns true if successful; otherwise returns false.

QFile 是使用此函数打开的,行为对于 close () is controlled by the handleFlags argument. If AutoCloseHandle is specified, and this function succeeds, then calling close () 会关闭采纳句柄。否则, close () 不会实际关闭文件,而仅刷新它。

警告: If the file handle is adopted from another process, you may not be able to use this QFile 采用 QFileInfo .

另请参阅 close ().

Permissions QFile:: permissions () const

Returns the complete OR-ed together combination of QFile::Permission for the file.

另请参阅 setPermissions () 和 setFileName ().

[static] Permissions QFile:: permissions (const QString & fileName )

这是重载函数。

Returns the complete OR-ed together combination of QFile::Permission for fileName .

[虚拟] qint64 QFile:: pos () const

重实现自 QIODevice::pos ().

[virtual protected] qint64 QFile:: readData ( char * data , qint64 len )

重实现自 QIODevice::readData ().

[virtual protected] qint64 QFile:: readLineData ( char * data , qint64 maxlen )

重实现自 QIODevice::readLineData ().

bool QFile:: remove ()

移除文件指定通过 fileName (). Returns true if successful; otherwise returns false.

文件被关闭,在移除它之前。

另请参阅 setFileName ().

[static] bool QFile:: remove (const QString & fileName )

这是重载函数。

移除文件指定通过 fileName 给定。

Returns true if successful; otherwise returns false.

另请参阅 remove ().

bool QFile:: rename (const QString & newName )

重命名文件目前指定通过 fileName () 到 newName . Returns true if successful; otherwise returns false.

若文件采用名称 newName already exists, rename() returns false (i.e., QFile 不会覆写它)。

文件关闭,在重命名之前。

另请参阅 setFileName ().

[static] bool QFile:: rename (const QString & oldName , const QString & newName )

这是重载函数。

重命名文件 oldName to newName . Returns true if successful; otherwise returns false.

若文件采用名称 newName already exists, rename () returns false (i.e., QFile 不会覆写它)。

另请参阅 rename ().

bool QFile:: resize ( qint64 sz )

设置文件大小 (以字节为单位) sz . Returns true if the file 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 更小,只需截取文件。

另请参阅 size () 和 setFileName ().

[static] bool QFile:: resize (const QString & fileName , qint64 sz )

这是重载函数。

fileName 到大小 (以字节为单位) sz . Returns true if the file if the resize succeeds; false otherwise. If sz > fileName 目前是新字节数将被设为 0,若 sz 更小,只需截取文件。

另请参阅 resize ().

[虚拟] bool QFile:: seek ( qint64 pos )

重实现自 QIODevice::seek ().

对于随机访问设备,此函数将当前位置设为 pos ,返回 true 当成功时,或 false 若出现错误。对于顺序设备,默认行为是什么都不做并返回 false。

Seeking beyond the end of a file: If the position is beyond the end of a file, then seek() shall not immediately extend the file. If a write is performed at this position, then the file shall 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.

[static] void QFile:: setDecodingFunction ( DecoderFn function )

设置 function for decoding 8-bit file names. The default uses the locale-specific 8-bit encoding.

警告: 此函数不 可重入 .

另请参阅 setEncodingFunction () 和 decodeName ().

[static] void QFile:: setEncodingFunction ( EncoderFn function )

设置 function for encoding Unicode file names. The default encodes in the locale-specific 8-bit encoding.

警告: 此函数不 可重入 .

另请参阅 encodeName () 和 setDecodingFunction ().

void QFile:: setFileName (const QString & name )

设置 name 为文件。名称可以没有路径、相对路径或绝对路径。

不要调用此函数,若文件已打开。

若文件名没有路径 (或相对路径),使用的路径将是应用程序的当前目录路径 open () 调用。

范例:

QFile file;
QDir::setCurrent("/tmp");
file.setFileName("readme.txt");
QDir::setCurrent("/home");
file.open(QIODevice::ReadOnly);      // opens "/home/readme.txt" under Unix
					

注意,目录分隔符 / 工作于由 Qt 支持的所有操作系统。

另请参阅 fileName (), QFileInfo ,和 QDir .

bool QFile:: setPermissions ( Permissions permissions )

将文件权限设为 permissions specified. Returns true if successful, or false if the permissions cannot be modified.

另请参阅 permissions () 和 setFileName ().

[static] bool QFile:: setPermissions (const QString & fileName , Permissions permissions )

这是重载函数。

设置权限为 fileName 文件到 permissions .

[虚拟] qint64 QFile:: size () const

重实现自 QIODevice::size ().

返回文件大小。

Unix 常规空文件 (如,那些在 /proc ),此函数返回 0;这种文件的内容是按需生成的,为响应调用 read ().

[static] QString QFile:: symLinkTarget (const QString & fileName )

返回符号链接 (或 Windows 快捷方式) 引用文件 (或目录) 的绝对路径指定通过 fileName ,或返回空字符串若 fileName 不对应于符号链接。

此名称可能不表示现有文件;它只是字符串。 QFile::exists () returns true if the symlink points to an existing file.

该函数在 Qt 4.2 引入。

QString QFile:: symLinkTarget () const

这是重载函数。

返回符号链接 (或 Windows 快捷方式) 指向文件 (或目录) 的绝对路径,或空字符串若对象不是符号链接。

此名称可能不表示现有文件;它只是字符串。 QFile::exists () returns true if the symlink points to an existing file.

该函数在 Qt 4.2 引入。

另请参阅 fileName () 和 setFileName ().

bool QFile:: unmap ( uchar * address )

取消映射内存 address .

Returns true if the unmap succeeds; false otherwise.

该函数在 Qt 4.4 引入。

另请参阅 map () 和 QAbstractFileEngine::supportsExtension ().

void QFile:: unsetError ()

把文件的错误设为 QFile::NoError .

另请参阅 error ().

[virtual protected] qint64 QFile:: writeData (const char * data , qint64 len )

重实现自 QIODevice::writeData ().