QFile Class ReferenceThe QFile class is an I/O device that operates on files. More... Almost all the functions in this class are reentrant when Qt is built with thread support. The exceptions are setEncodingFunction(), setDecodingFunction(), and setErrorString(). #include <qfile.h> Inherits QIODevice. Public Members
Static Public Members
Important Inherited Members
Protected Members
Detailed DescriptionThe 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 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. Member Type Documentation
|
Flag | Meaning |
---|---|
IO_Raw | Raw (non-buffered) file access. |
IO_ReadOnly | Opens the file in read-only mode. |
IO_WriteOnly | Opens the file in write-only mode. If this flag is used with another flag, e.g. IO_ReadOnly or IO_Raw or IO_Append, the file is not truncated; but if used on its own (or with IO_Truncate), the file is truncated. |
IO_ReadWrite | Opens the file in read/write mode, equivalent to (IO_ReadOnly | IO_WriteOnly). |
IO_Append | Opens the file in append mode. (You must actually use (IO_WriteOnly | IO_Append) to make the file writable and to go into append mode.) This mode is very useful when you want to write something to a log file. The file index is set to the end of the file. Note that the result is undefined if you position the file index manually using at() in append mode. |
IO_Truncate | Truncates the file. |
IO_Translate | Enables carriage returns and linefeed translation for text files under Windows. |
The raw access mode is best when I/O is block-operated using a 4KB block size or greater. Buffered access works better when reading small portions of data at a time.
Warning: 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. at(), 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" ); f1.open( IO_Raw | IO_ReadWrite ); QFile f2( "readme.txt" ); f2.open( IO_ReadOnly | IO_Translate ); QFile f3( "audit.log" ); f3.open( IO_WriteOnly | IO_Append );
See also name(), close(), isOpen(), and flush().
Examples: application/application.cpp, chart/chartform_files.cpp, distributor/distributor.ui.h, helpviewer/helpwindow.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.
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. Any terminating newline is not stripped.
This function is only efficient 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, 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 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.
Warning: This function is not reentrant.
Sets the function for decoding 8-bit file names to f. The default uses the locale-specific 8-bit encoding.
See also encodeName() and decodeName().
Warning: This function is not reentrant.
Sets the function for encoding Unicode file names to f. The default encodes in the locale-specific 8-bit encoding.
See also encodeName().
Warning: This function is not reentrant.
Sets the error string returned by the errorString() function to str.
See also errorString() and QIODevice::status().
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 file; QDir::setCurrent( "/tmp" ); file.setName( "readme.txt" ); QDir::setCurrent( "/home" ); file.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 an error occurred.
Reimplemented from QIODevice.
This file is part of the Qt toolkit. Copyright © 1995-2003 Trolltech. All Rights Reserved.
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com