Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The QFile class is an I/O device that operates on files. More...
#include <QFile>
Inherits QIODevice.
Note: All the functions in this class are reentrant, except setEncodingFunction() and setDecodingFunction().
The QFile class is an I/O device that operates on files.
QFile is an I/O device for reading and writing binary and text files. A QFile may be used by itself or, more conveniently, with a QDataStream or QTextStream.
The file name is usually passed in the constructor, but it can be changed with setFileName(). You can check for a file's existence with exists(), and remove a file with remove().
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 read with readBlock() and readLine(), and write with writeBlock(). QFile also supports getch(), ungetch(), and putch().
The size of the file is returned by size(). You can get the current file position or move to a new file position using the at() functions. If you've reached the end of the file, atEnd() returns true. The file handle is returned by handle().
The following example uses QTextStream to read a text file line by line, printing each line with a line number:
QStringList lines; QFile file("file.txt"); if (file.open(QIODevice::ReadOnly)) { QTextStream stream(&file); QString line; int i = 1; while (!stream.atEnd()) { line = stream.readLine(); // line of text excluding '\n' printf("%3d: %s\n", i++, line.latin1()); lines += line; } file.close(); }
Writing text is just as easy. The following example shows how to write the data we read in the previous example to a file:
QFile file("file.txt"); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); QStringList::ConstIterator i = lines.constBegin(); for (; i != lines.constEnd(); ++i) stream << *i << "\n"; file.close(); }
The QFileInfo class holds detailed information about a file, such as access permissions, file dates and file types.
The QDir class manages directories and lists of file names.
When you use QFile, QFileInfo, and 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 do your own file I/O on Unix, you should convert file names using the encodeName() and decodeName() functions to convert the file name into the local encoding.
The conversion scheme can be changed using setEncodingFunction(). This might be useful if you wish to give the user an option to store file names in UTF-8, for example, but be aware that such file names would probably then be unrecognizable when seen by other programs.
On Windows NT/2000, Unicode file names are supported directly in the file system and this function should be avoided. On Windows 95, non-Latin1 locales are not supported.
See also QDataStream and QTextStream.
This is used by QFile::setDecodingFunction() to specify how file names are converted from the local encoding to Unicode.
This is used by QFile::setEncodingFunction() to specify how Unicode file names are converted to the appropriate local encoding.
Constructs a QFile with no name.
Constructs a QFile with a file name name.
See also setFileName().
Destroys the file object, closing it if necessary.
This does the reverse of QFile::encodeName() using localFileName.
See also setDecodingFunction().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns the Unicode version of the given localFileName. See encodeName() for details.
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.
See also decodeName() and setEncodingFunction().
Returns true if the file specified by fileName exists; otherwise returns false.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns true if the file specified by fileName() exists; otherwise returns false.
See also fileName() and setFileName().
Returns the name set by setFileName().
See also setFileName() and QFileInfo::fileName().
Returns the file handle of the file.
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 as well.
If the file is not open, or there is an error, handle() returns -1.
See also QSocketNotifier.
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(QIODevice::WriteOnly, stderr); file.writeBlock(msg, qstrlen(msg)); // write to stderr file.close(); }
When a QFile is opened using this function, close() does not actually close the file, but only flushes it.
Warning: If fh is stdin, stdout, or stderr, you may not be able to seek(). See QIODevice::isSequentialAccess() for more information.
See also close().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Opens the existing file descripter fd in the given mode. Returns true if successful; otherwise returns false.
When a 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.
Warning: If fd is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able to seek(). size() is set to LLONG_MAX (in limits.h).
See also close().
Reads a line of text.
Reads bytes from the file into the string until end-of-line or the maximum number bytes have been read, whichever occurs first. Returns the number of bytes read, or -1 if there was an error (e.g. end of file). Any terminating newline is not stripped.
This function is only efficient for buffered files. Avoid using readLine() for files that have been opened with the QIODevice::Raw flag.
Note that the string is read as plain Latin1 bytes, not Unicode.
See also readBlock() and QTextStream::readLine().
Removes the file specified by fileName(). Returns true if successful; otherwise returns false.
The file is closed before it is removed.
See also setFileName().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Removes the file specified by the fileName given.
Returns true if successful; otherwise returns false.
See also remove().
Renames the file currently specified by fileName() to newName. Returns true if successful; otherwise returns false.
The file is closed before it is renamed.
See also setFileName().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Renames the file oldName to newName. Returns true if successful; otherwise returns false.
See also rename().
Sets the function for decoding 8-bit file names. The default uses the locale-specific 8-bit encoding.
Warning: This function is not reentrant.
See also encodeName() and decodeName().
Sets the function for encoding Unicode file names. The default encodes in the locale-specific 8-bit encoding.
Warning: This function is not reentrant.
See also encodeName().
Sets the name of the file. The name can have no path, a relative path, or an absolute 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); // opens "/home/readme.txt" under Unix
Note that the directory separator "/" works for all operating systems supported by Qt.
See also fileName(), QFileInfo, and QDir.
Copyright © 2004 Trolltech. | Trademarks | Qt 4.0.0-tp1 |