Eneboo - Documentación para desarrolladores
|
The QFile class is an I/O device that operates on files. Más...
#include <qfile.h>
Tipos públicos | |
typedef QCString(* | EncoderFn )(const QString &fileName) |
typedef QString(* | DecoderFn )(const QCString &localfileName) |
typedef QCString(* | EncoderFn )(const QString &fileName) |
typedef QString(* | DecoderFn )(const QCString &localfileName) |
Métodos públicos | |
QFile () | |
QFile (const QString &name) | |
~QFile () | |
QString | name () const |
void | setName (const QString &name) |
bool | exists () const |
bool | remove () |
bool | open (int) |
bool | open (int, FILE *) |
bool | open (int, int) |
void | close () |
void | flush () |
Offset | size () const |
Offset | at () const |
bool | at (Offset) |
bool | atEnd () const |
Q_LONG | readBlock (char *data, Q_ULONG len) |
Q_LONG | writeBlock (const char *data, Q_ULONG len) |
Q_LONG | writeBlock (const QByteArray &data) |
Q_LONG | readLine (char *data, Q_ULONG maxlen) |
Q_LONG | readLine (QString &, Q_ULONG maxlen) |
int | getch () |
int | putch (int) |
int | ungetch (int) |
int | handle () const |
QString | errorString () const |
QFile () | |
QFile (const QString &name) | |
~QFile () | |
QString | name () const |
void | setName (const QString &name) |
bool | exists () const |
bool | remove () |
bool | open (int) |
bool | open (int, FILE *) |
bool | open (int, int) |
void | close () |
void | flush () |
Offset | size () const |
Offset | at () const |
bool | at (Offset) |
bool | atEnd () const |
Q_LONG | readBlock (char *data, Q_ULONG len) |
Q_LONG | writeBlock (const char *data, Q_ULONG len) |
Q_LONG | writeBlock (const QByteArray &data) |
Q_LONG | readLine (char *data, Q_ULONG maxlen) |
Q_LONG | readLine (QString &, Q_ULONG maxlen) |
int | getch () |
int | putch (int) |
int | ungetch (int) |
int | handle () const |
QString | errorString () const |
Métodos públicos estáticos | |
static QCString | encodeName (const QString &fileName) |
static QString | decodeName (const QCString &localFileName) |
static void | setEncodingFunction (EncoderFn) |
static void | setDecodingFunction (DecoderFn) |
static bool | exists (const QString &fileName) |
static bool | remove (const QString &fileName) |
static QCString | encodeName (const QString &fileName) |
static QString | decodeName (const QCString &localFileName) |
static void | setEncodingFunction (EncoderFn) |
static void | setDecodingFunction (DecoderFn) |
static bool | exists (const QString &fileName) |
static bool | remove (const QString &fileName) |
Métodos protegidos | |
void | setErrorString (const QString &) |
void | setErrorString (const QString &) |
Atributos protegidos | |
QString | fn |
FILE * | fh |
int | fd |
Offset | length |
bool | ext_f |
QFilePrivate * | d |
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 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.
typedef QString(* QFile::DecoderFn)(const QCString &localfileName) |
typedef QString(* QFile::DecoderFn)(const QCString &localfileName) |
typedef QCString(* QFile::EncoderFn)(const QString &fileName) |
typedef QCString(* QFile::EncoderFn)(const QString &fileName) |
QFile::QFile | ( | ) |
Constructs a QFile with no name.
QFile::QFile | ( | const QString & | name | ) |
QFile::QFile | ( | ) |
QFile::QFile | ( | const QString & | name | ) |
QFile::~QFile | ( | ) |
QIODevice::Offset QFile::at | ( | ) | const [inline, virtual] |
Reimplementado de QIODevice.
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. Sets the file index to pos. Returns TRUE if successful; otherwise returns FALSE.
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.
IO_Append
specifier.Sets the file index to pos. Returns TRUE if successful; otherwise returns FALSE.
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.
IO_Append
specifier.Reimplementado de QIODevice.
Offset QFile::at | ( | ) | const [virtual] |
bool QFile::atEnd | ( | ) | const [virtual] |
bool QFile::atEnd | ( | ) | const [virtual] |
Virtual function that returns TRUE if the I/O device position is at the end of the input; otherwise returns FALSE.
Reimplementado de QIODevice.
void QFile::close | ( | void | ) | [virtual] |
Closes an open file.
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.
Implementa QIODevice.
void QFile::close | ( | void | ) | [virtual] |
This does the reverse of QFile::encodeName() using localFileName.
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 the file name using this function. 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.
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.
QString QFile::errorString | ( | ) | const |
Returns a human-readable description of the reason of an error that occurred on the device. The error described by the string corresponds to changes of QIODevice::status(). If the status is reset, the error string is also reset.
The returned strings are not translated with the QObject::tr() or QApplication::translate() functions. They are marked as translatable strings in the "QFile" context. Before you show the string to the user you should translate it first, for example:
QFile f( "address.dat" ); if ( !f.open( IO_ReadOnly ) { QMessageBox::critical( this, tr("Open failed"), tr("Could not open file for reading: %1").arg( qApp->translate("QFile",f.errorString()) ) ); return; }
QString QFile::errorString | ( | ) | const |
Returns TRUE if the file given by fileName exists; otherwise returns FALSE.
bool QFile::exists | ( | ) | const |
bool QFile::exists | ( | ) | const |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. Returns TRUE if this file exists; otherwise returns FALSE.
void QFile::flush | ( | ) | [virtual] |
Flushes an open I/O device.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
void QFile::flush | ( | ) | [virtual] |
int QFile::getch | ( | ) | [virtual] |
int QFile::getch | ( | ) | [virtual] |
int QFile::handle | ( | ) | const |
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 (ie. 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.
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(), as well as with QSocketNotifier.
If the file is not open or there is an error, handle() returns -1.
int QFile::handle | ( | ) | const |
QString QFile::name | ( | ) | const [inline] |
Returns the name set by setName().
QString QFile::name | ( | ) | const |
Opens the I/O device using the specified mode. Returns TRUE if the device was successfully opened; otherwise returns FALSE.
The mode parameter mode must be an OR'ed combination of the following flags. Mode flags Meaning IO_Raw
specifies raw (unbuffered) file access. IO_ReadOnly
opens a file in read-only mode. IO_WriteOnly
opens a file in write-only mode. IO_ReadWrite
opens a file in read/write mode. IO_Append
sets the file index to the end of the file. IO_Truncate
truncates the file. IO_Translate
enables carriage returns and linefeed translation for text files under MS-DOS, Windows and Macintosh. On Unix systems this flag has no effect. Use with caution as it will also transform every linefeed written to the file into a CRLF pair. This is likely to corrupt your file if you write write binary data. Cannot be combined with IO_Raw
.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Opens the file specified by the file name currently set, using the mode m. Returns TRUE if successful, otherwise FALSE.
IO_Raw IO_ReadOnly IO_WriteOnly IO_ReadWrite IO_Append IO_Truncate IO_Translate
The mode parameter m must be a combination of the following flags: 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.
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 );
Implementa QIODevice.
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. 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.
stdin
, stdout
, stderr
, you may not be able to seek. See QIODevice::isSequentialAccess() for more information.Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. 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.
INT_MAX
(in limits.h).Q_LONG QFile::readBlock | ( | char * | p, |
Q_ULONG | len | ||
) | [virtual] |
Reads at most len bytes from the file into p and returns the number of bytes actually read.
Returns -1 if a serious error occurred.
Implementa QIODevice.
Q_LONG QFile::readBlock | ( | char * | data, |
Q_ULONG | maxlen | ||
) | [virtual] |
Reads at most maxlen bytes from the I/O device into data and returns the number of bytes actually read.
This function should return -1 if a fatal error occurs and should return 0 if there are no bytes to read.
The device must be opened for reading, and data must not be 0.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Q_LONG QFile::readLine | ( | char * | p, |
Q_ULONG | maxlen | ||
) | [virtual] |
Reads a line of text.
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.
Reimplementado de QIODevice.
Q_LONG QFile::readLine | ( | char * | data, |
Q_ULONG | maxlen | ||
) | [virtual] |
Reads a line of text, (or up to maxlen bytes if a newline isn't encountered) plus a terminating '\0' into data. If there is a newline at the end if the line, it is not stripped.
Returns the number of bytes read including the terminating '\0', or -1 if an error occurred.
This virtual function can be reimplemented much more efficiently by the most subclasses.
Reimplementado de QIODevice.
Q_LONG QFile::readLine | ( | QString & | s, |
Q_ULONG | maxlen | ||
) |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. 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.
Q_LONG QFile::readLine | ( | QString & | , |
Q_ULONG | maxlen | ||
) |
bool QFile::remove | ( | void | ) |
Removes the file specified by the file name currently set. Returns TRUE if successful; otherwise returns FALSE.
The file is closed before it is removed.
bool QFile::remove | ( | ) |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. Removes the file fileName. Returns TRUE if successful, otherwise FALSE.
static void QFile::setDecodingFunction | ( | DecoderFn | ) | [static] |
void QFile::setDecodingFunction | ( | DecoderFn | f | ) | [static] |
Sets the function for decoding 8-bit file names to f. The default uses the locale-specific 8-bit encoding.
static void QFile::setEncodingFunction | ( | EncoderFn | ) | [static] |
void QFile::setEncodingFunction | ( | EncoderFn | f | ) | [static] |
Sets the function for encoding Unicode file names to f. The default encodes in the locale-specific 8-bit encoding.
void QFile::setErrorString | ( | const QString & | str | ) | [protected] |
Sets the error string returned by the errorString() function to str.
void QFile::setErrorString | ( | const QString & | ) | [protected] |
void QFile::setName | ( | const QString & | name | ) |
void QFile::setName | ( | const QString & | name | ) |
Sets the name of the file to name. 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 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.
QIODevice::Offset QFile::size | ( | ) | const [virtual] |
Offset QFile::size | ( | ) | const [virtual] |
Q_LONG QFile::writeBlock | ( | const char * | p, |
Q_ULONG | len | ||
) | [virtual] |
Writes len bytes from p to the file and returns the number of bytes actually written.
Returns -1 if a serious error occurred.
Implementa QIODevice.
Q_LONG QFile::writeBlock | ( | const char * | data, |
Q_ULONG | len | ||
) | [virtual] |
Writes len bytes from data to the I/O device and returns the number of bytes actually written.
This function should return -1 if a fatal error occurs.
This virtual function must be reimplemented by all subclasses.
Implementa QIODevice.
Q_LONG QFile::writeBlock | ( | const QByteArray & | data | ) | [inline] |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. This convenience function is the same as calling writeBlock( data.data(), data.size() ).
Reimplementado de QIODevice.
Q_ULONG QFile::writeBlock | ( | const QByteArray & | data | ) | [inline] |
Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta.
Reimplementado de QIODevice.
QFilePrivate * QFile::d [protected] |
bool QFile::ext_f [protected] |
FILE * QFile::fh [protected] |
Offset QFile::length [protected] |