#include <qfile.h>
Inherits QIODevice.
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 can be changed with setName(). 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().
Here is a code fragment that uses QTextStream to read a text file line by line. It prints each line with a line number.
QStringList lines;
QFile file( "file.txt" );
if ( file.open( IO_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 into the string list from the previous example:
QFile file( "file.txt" );
if ( file.open( IO_WriteOnly ) ) {
QTextStream stream( &file );
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
stream << *it << "\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.
Qt uses Unicode file names. If you want to do your own I/O on Unix systems you may want to use encodeName() (and decodeName()) to convert the file name into the local encoding.
See also QDataStream, QTextStream, and Input/Output and Networking.
See also size().
Example: distributor/distributor.ui.h.
The file is not closed if it was opened with an existing file handle. If the existing file handle is a FILE*, the file is flushed. If the existing file handle is an int file descriptor, nothing is done to the file.
Some "write-behind" filesystems may report an unspecified error on closing the file. These errors only indicate that something may have gone wrong since the previous open(). In such a case status() reports IO_UnspecifiedError after close(), otherwise IO_Ok.
See also open() and flush().
Examples:
See also setDecodingFunction().
Example: distributor/distributor.ui.h.
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.
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, etc., but be aware that such file names would probably then be unrecognizable when seen by other programs.
See also decodeName().