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 類似,可以使用操作符 <<() 寫入數據,和使用操作符 >>() 讀迴它。見類文檔編製,瞭解細節。

當使用 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 ().