Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QFile class is an I/O device that operates on files. More...
#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.
QFile f("file.txt"); if ( f.open(IO_ReadOnly) ) { // file opened successfully QTextStream t( &f ); // use a text stream QString s; int n = 1; while ( !t.eof() ) { // until end of file... s = t.readLine(); // line of text excluding '\n' printf( "%3d: %s\n", n++, s.latin1() ); } f.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.
This is used by QFile::setDecodingFunction().
This is used by QFile::setEncodingFunction().
See also setName().
Example:
QFile f( "data.bin" ); f.open( IO_ReadOnly ); // index set to 0 f.at( 100 ); // set index to 100 f.at( f.at()+50 ); // set index to 150 f.at( f.size()-80 ); // set index to 80 before EOF f.close();
Use at() without arguments to retrieve the file offset.
Warning: The result is undefined if the file was open()'ed using the IO_Append specifier.
Reimplemented from QIODevice.
Returns the position in the file.
See also size().
Reimplemented from QIODevice.
See also size().
Reimplemented from QIODevice.
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.
Examples: addressbook/centralwidget.cpp, application/application.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, qdir/qdir.cpp, qwerty/qwerty.cpp and xml/outliner/outlinetree.cpp.
Reimplemented from QIODevice.
See also setDecodingFunction().
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 ware that such file names would probably then be unrecognizable when seen by other programs.
See also decodeName().
Examples: dirview/dirview.cpp and helpviewer/helpwindow.cpp.
Returns TRUE if this file exists; otherwise returns FALSE.
See also name().
close() also flushes the file buffer.
Reimplemented from QIODevice.
Returns the byte/character read, or -1 if the end of the file has been reached.
See also putch() and ungetch().
Reimplemented from QIODevice.
This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(), as well as with QSocketNotifier.
If the file is not open or there is an error, handle() returns -1.
See also QSocketNotifier.
See also setName() and QFileInfo::fileName().
The mode parameter m must be a combination of the following flags:
The raw access mode is best when I/O is block-operated using 4kB block size or greater. Buffered access works better when reading small portions of data at a time.
Important: When working with buffered files, data may not be written to the file at once. Call flush() to make sure that the data is really written.
Warning: If you have a buffered file opened for both reading and writing you must not perform an input operation immediately after an output operation or vice versa. You should always call flush() or a file positioning operation, e.g. seek(), between input and output operations, otherwise the buffer may contain garbage.
If the file does not exist and IO_WriteOnly or IO_ReadWrite is specified, it is created.
Example:
QFile f1( "/tmp/data.bin" ); QFile f2( "readme.txt" ); f1.open( IO_Raw | IO_ReadWrite | IO_Append ); f2.open( IO_ReadOnly | IO_Translate );
See also name(), close(), isOpen() and flush().
Examples: action/application.cpp, application/application.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, qdir/qdir.cpp, qwerty/qwerty.cpp and xml/outliner/outlinetree.cpp.
Reimplemented from QIODevice.
Opens a file in the mode m using an existing file handle f. Returns TRUE if successful, otherwise FALSE.
Example:
#include <stdio.h> void printError( const char* msg ) { QFile f; f.open( IO_WriteOnly, stderr ); f.writeBlock( msg, qstrlen(msg) ); // write to stderr f.close(); }
When a QFile is opened using this function, close() does not actually close the file, only flushes it.
Warning: If f is stdin, stdout, stderr, you may not be able to seek. See QIODevice::isSequentialAccess() for more information.
See also close().
Opens a file in the mode m using an existing file descriptor f. Returns TRUE if successful, otherwise 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 f is one of 0 (stdin), 1 (stdout) or 2 (stderr), you may not be able to seek. size() is set to INT_MAX (in limits.h).
See also close().
Returns ch, or -1 if some error occurred.
See also getch() and ungetch().
Reimplemented from QIODevice.
Returns -1 if a serious error occurred.
Warning: We have experienced problems with some C libraries when a buffered file is opened for both reading and writing. If a read operation takes place immediately after a write operation, the read buffer contains garbage data. Worse, the same garbage is written to the file. Calling flush() before readBlock() solved this problem.
See also writeBlock().
Example: qwerty/qwerty.cpp.
Reimplemented from QIODevice.
Reads bytes from the file into the char* p, until end-of-line or maxlen bytes have been read, whichever occurs first. Returns the number of bytes read, or -1 if there was an error. The terminating newline is not stripped.
This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.
See also readBlock() and QTextStream::readLine().
Reimplemented from QIODevice.
Reads a line of text.
Reads bytes from the file into string s, until end-of-line or maxlen bytes have been read, whichever occurs first. Returns the number of bytes read, or -1 if there was an error.g. end of file. The terminating newline is not stripped.
This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.
Note that the string is read as plain Latin1 bytes, not Unicode.
See also readBlock() and QTextStream::readLine().
The file is closed before it is removed.
Removes the file fileName. Returns TRUE if successful, otherwise FALSE.
See also encodeName() and decodeName().
See also encodeName().
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 whatever the application's current directory path is at the time of the open() call.
Example:
QFile f; QDir::setCurrent( "/tmp" ); f.setName( "readme.txt" ); QDir::setCurrent( "/home" ); f.open( IO_ReadOnly ); // opens "/home/readme.txt" under Unix
Note that the directory separator "/" works for all operating systems supported by Qt.
See also name(), QFileInfo and QDir.
See also at().
Example: table/statistics/statistics.cpp.
Reimplemented from QIODevice.
This function is normally called to "undo" a getch() operation.
Returns ch, or -1 if some error occurred.
Reimplemented from QIODevice.
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|