Member Function Documentation
QFile::QFile(const QString & name)
Constructs a new file object to represent the file with the given name.
QFile::QFile(QObject * parent)
Constructs a new file object with the given parent.
QFile::QFile(const QString & name, QObject * parent)
Constructs a new file object with the given parent to represent the file with the specified name.
QFile::~QFile()
Destroys the file object, closing it if necessary.
bool QFile::copy(const QString & newName)
Copies the file currently specified by fileName() to a file called newName. Returns true if successful; otherwise returns false.
Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
The source file is closed before it is copied.
See also setFileName().
bool QFile::copy(const QString & fileName, const QString & newName) [static]
This is an overloaded function.
Copies the file fileName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
See also rename().
QString QFile::decodeName(const QByteArray & localFileName) [static]
This does the reverse of QFile::encodeName() using localFileName.
See also encodeName().
QString QFile::decodeName(const char * localFileName) [static]
This is an overloaded function.
Returns the Unicode version of the given localFileName. See encodeName() for details.
QByteArray QFile::encodeName(const QString & fileName) [static]
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.
See also decodeName().
bool QFile::exists(const QString & fileName) [static]
Returns true if the file specified by fileName exists; otherwise returns false.
bool QFile::exists() const
This is an overloaded function.
Returns true if the file specified by fileName() exists; otherwise returns false.
See also fileName() and setFileName().
QString QFile::fileName() const [virtual]
Reimplemented from QFileDevice::fileName().
Returns the name set by setFileName() or to the QFile constructors.
See also setFileName() and QFileInfo::fileName().
bool QFile::link(const QString & linkName)
Creates a link named linkName that points to the file currently specified by 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.
This function will not overwrite an already existing entity in the file system; in this case, link() will return false and set error() to return RenameError.
Note: To create a valid link on Windows, linkName must have a .lnk file extension.
See also setFileName().
bool QFile::link(const QString & fileName, const QString & linkName) [static]
This is an overloaded function.
Creates a link named linkName that points to the file 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.
See also link().
bool QFile::open(OpenMode mode) [virtual]
Reimplemented from QIODevice::open().
Opens the file using OpenMode mode, returning true if successful; otherwise false.
The mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
See also QIODevice::OpenMode and setFileName().
bool QFile::open(FILE * fh, OpenMode mode, FileHandleFlags handleFlags = DontCloseHandle)
This is an overloaded function.
Opens the existing file handle fh in the given mode. Returns true if successful; otherwise returns false.
Example:
#include <stdio.h>
void printError(const char* msg)
{
QFile file;
file.open(stderr, QIODevice::WriteOnly);
file.write(msg, qstrlen(msg));
file.close();
}
When a QFile is opened using this function, behaviour of close() is controlled by the AutoCloseHandle flag. If AutoCloseHandle is specified, and this function succeeds, then calling close() closes the adopted handle. Otherwise, close() does not actually close the file, but only flushes it.
Warning:
- If fh does not refer to a regular file, e.g., it is stdin, stdout, or stderr, you may not be able to seek(). size() returns 0 in those cases. See QIODevice::isSequential() for more information.
- Since this function opens the file without specifying the file name, you cannot use this QFile with a QFileInfo.
Note: For Windows CE you may not be able to call resize().
Note for the Windows Platform
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. Sequential devices, such as stdin and stdout, are unaffected by this limitation.
You need to enable support for console applications in order to use the stdin, stdout and stderr streams at the console. To do this, add the following declaration to your application's project file:
CONFIG += console
See also close() and qmake Variable Reference.
bool QFile::open(int fd, OpenMode mode, FileHandleFlags handleFlags = DontCloseHandle)
This is an overloaded function.
Opens the existing file descriptor fd in the given mode. Returns true if successful; otherwise returns false.
When a QFile is opened using this function, behaviour of close() is controlled by the AutoCloseHandle flag. If AutoCloseHandle is specified, and this function succeeds, then calling close() closes the adopted handle. Otherwise, close() does not actually close the file, but only flushes it.
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.
Warning: If fd is not a regular file, e.g, it is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able to seek(). In those cases, size() returns 0. See QIODevice::isSequential() for more information.
Warning: For Windows CE you may not be able to call seek(), setSize(), fileTime(). size() returns 0.
Warning: Since this function opens the file without specifying the file name, you cannot use this QFile with a QFileInfo.
See also close().
Permissions QFile::permissions() const [virtual]
Reimplemented from QFileDevice::permissions().
See also setPermissions().
Permissions QFile::permissions(const QString & fileName) [static]
This is an overloaded function.
Returns the complete OR-ed together combination of QFile::Permission for fileName.
bool QFile::remove()
Removes the file specified by fileName(). Returns true if successful; otherwise returns false.
The file is closed before it is removed.
See also setFileName().
bool QFile::remove(const QString & fileName) [static]
This is an overloaded function.
Removes the file specified by the fileName given.
Returns true if successful; otherwise returns false.
See also remove().
bool QFile::rename(const QString & newName)
Renames the file currently specified by fileName() to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).
The file is closed before it is renamed.
If the rename operation fails, Qt will attempt to copy this file's contents to newName, and then remove this file, keeping only newName. If that copy operation fails or this file can't be removed, the destination file newName is removed to restore the old state.
See also setFileName().
bool QFile::rename(const QString & oldName, const QString & newName) [static]
This is an overloaded function.
Renames the file oldName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).
See also rename().
bool QFile::resize(qint64 sz) [virtual]
Reimplemented from QFileDevice::resize().
bool QFile::resize(const QString & fileName, qint64 sz) [static]
This is an overloaded function.
Sets fileName to size (in bytes) sz. Returns true if the file if the resize succeeds; false otherwise. If sz is larger than fileName currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
See also resize().
void QFile::setFileName(const QString & name)
Sets the name of the file. The name can have no path, a relative path, or an absolute path.
Do not call this function if the file has already been opened.
If the file name has no path or a relative path, the path used will be the application's current directory path at the time of the open() call.
Example:
QFile file;
QDir::setCurrent("/tmp");
file.setFileName("readme.txt");
QDir::setCurrent("/home");
file.open(QIODevice::ReadOnly);
Note that the directory separator "/" works for all operating systems supported by Qt.
See also fileName(), QFileInfo, and QDir.
bool QFile::setPermissions(Permissions permissions) [virtual]
Reimplemented from QFileDevice::setPermissions().
Sets the permissions for the file to the permissions specified. Returns true if successful, or false if the permissions cannot be modified.
See also permissions() and setFileName().
bool QFile::setPermissions(const QString & fileName, Permissions permissions) [static]
This is an overloaded function.
Sets the permissions for fileName file to permissions.
qint64 QFile::size() const [virtual]
Reimplemented from QIODevice::size().
QString QFile::symLinkTarget(const QString & fileName) [static]
Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by fileName, or returns an empty string if the fileName does not correspond to a symbolic link.
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
This function was introduced in Qt 4.2.
QString QFile::symLinkTarget() const
This is an overloaded function.
Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
This function was introduced in Qt 4.2.
See also fileName() and setFileName().