QtBase
v6.3.1
|
The QFile class provides an interface for reading from and writing to files. More...
#include <qfile.h>
Friends | |
class | QTemporaryFile |
The QFile class provides an interface for reading from and writing to files.
\inmodule QtCore
\reentrant
QFile is an I/O device for reading and writing text and binary files and \l{The Qt Resource System}{resources}. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream.
The file name is usually passed in the constructor, but it can be set at any time using setFileName(). QFile expects the file separator to be '/' regardless of operating system. The use of other separators (e.g., '\') is not supported.
You can check for a file's existence using exists(), and remove a file using remove(). (More advanced file system related operations are provided by QFileInfo and QDir.)
The file is opened with open(), closed with close(), and flushed with flush(). Data is usually read and written using QDataStream or QTextStream, but you can also call the QIODevice-inherited functions read(), readLine(), readAll(), write(). QFile also inherits getChar(), putChar(), and ungetChar(), which work one character at a time.
The size of the file is returned by size(). You can get the current file position using pos(), or move to a new file position using seek(). If you've reached the end of the file, atEnd() returns true
.
QFile::QFile | ( | ) |
|
explicit |
QFile::~QFile | ( | ) |
|
protected |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Copies the file named fileName to newName.
Definition at line 876 of file qfile.cpp.
Copies the file named fileName() to newName.
Definition at line 766 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the Unicode version of the given localFileName. See encodeName() for details.
Definition at line 167 of file qfile.h.
|
inlinestatic |
This does the reverse of QFile::encodeName() using localFileName.
Definition at line 163 of file qfile.h.
|
inlinestatic |
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.
Definition at line 159 of file qfile.h.
bool QFile::exists | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns true
if the file specified by fileName() exists; otherwise returns false
.
Definition at line 376 of file qfile.cpp.
|
overridevirtual |
Returns the name set by setFileName() or to the QFile constructors.
Reimplemented from QFileDevice.
Reimplemented in QTemporaryFile.
Definition at line 302 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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
.
Definition at line 752 of file qfile.cpp.
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 \l{QFile::}{error()} to return \l{QFile::}{RenameError}.
{.lnk} file extension.Definition at line 724 of file qfile.cpp.
bool QFile::moveToTrash | ( | ) |
Moves the file specified by fileName() to the trash. Returns true
if successful, and sets the fileName() to the path at which the file can be found within the trash; otherwise returns false
.
Definition at line 492 of file qfile.cpp.
Moves the file specified by fileName() to the trash. Returns true
if successful, and sets pathInTrash (if provided) to the path at which the file can be found within the trash; otherwise returns false
.
Definition at line 529 of file qfile.cpp.
bool QFile::open | ( | FILE * | fh, |
OpenMode | mode, | ||
FileHandleFlags | handleFlags = DontCloseHandle |
||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Opens the existing file handle fh in the given mode. handleFlags may be used to specify additional options. Returns true
if successful; otherwise returns false
.
Example:
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:} \list 1
stdin
, stdout
, or stderr
, you may not be able to seek(). size() returns 0
in those cases. See QIODevice::isSequential() for more information. {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:
Definition at line 1012 of file qfile.cpp.
bool QFile::open | ( | int | fd, |
OpenMode | mode, | ||
FileHandleFlags | handleFlags = DontCloseHandle |
||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Opens the existing file descriptor fd in the given mode. handleFlags may be used to specify additional options. 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.
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.Definition at line 1064 of file qfile.cpp.
|
override |
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.
Definition at line 897 of file qfile.cpp.
bool QFile::open | ( | OpenMode | flags, |
Permissions | permissions | ||
) |
|
overridevirtual |
\reimp
Reimplemented from QFileDevice.
Definition at line 1123 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the complete OR-ed together combination of QFile::Permission for fileName.
Definition at line 1136 of file qfile.cpp.
bool QFile::remove | ( | ) |
Removes the file specified by fileName(). Returns true
if successful; otherwise returns false
.
The file is closed before it is removed.
Definition at line 444 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Removes the file specified by the fileName given.
Returns true
if successful; otherwise returns false
.
Definition at line 475 of file qfile.cpp.
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.
Definition at line 558 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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).
Definition at line 702 of file qfile.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets fileName to size (in bytes) sz. Returns true
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.
Definition at line 1115 of file qfile.cpp.
|
overridevirtual |
\reimp
Reimplemented from QFileDevice.
Definition at line 1096 of file qfile.cpp.
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:
Note that the directory separator "/" works for all operating systems supported by Qt.
Definition at line 327 of file qfile.cpp.
|
overridevirtual |
Sets the permissions for the file to the permissions specified. Returns true
if successful, or false
if the permissions cannot be modified.
Reimplemented from QFileDevice.
Definition at line 1152 of file qfile.cpp.
|
overridevirtual |
QString QFile::symLinkTarget | ( | ) | const |
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.
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.
Definition at line 429 of file qfile.cpp.
|
friend |