Qt logo

QFile Class Reference


The QFile class is an I/O device that operates on files. More...

#include <qfile.h>

Inherits QIODevice.

List of all member functions.

Public Members

Static Public Members


Detailed Description

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 (readBlock and writeBlock) or by more conveniently using QDataStream or QTextStream.

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++, (const char *)s );
        }
        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.

See also: QDataStream and QTextStream.

Examples: application/application.cpp


Member Function Documentation

QFile::QFile ()

Constructs a QFile with no name.

QFile::QFile ( const char * name )

Constructs a QFile with a file name name.

See also: setName().

QFile::~QFile ()

Destroys a QFile. Calls close().

bool QFile::at ( int pos ) [virtual]

Sets the file index to pos. Returns TRUE if successful, otherwise 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();

Warning: The result is undefined if the file was opened using the IO_Append specifier.

See also: size() and open().

Reimplemented from QIODevice.

int QFile::at () const [virtual]

Returns the file index.

See also: size().

Reimplemented from QIODevice.

bool QFile::atEnd () const [virtual]

Returns TRUE if the end of file has been reached, otherwise FALSE.

See also: size().

Reimplemented from QIODevice.

void QFile::close () [virtual]

Closes an open file.

The file is closed even if it was opened with an existing file handle or a file descriptor, except that stdin, stdout and stderr are never closed.

See also: open() and flush().

Examples: application/application.cpp

Reimplemented from QIODevice.

bool QFile::exists () const

Returns TRUE if the file exists, otherwise FALSE.

See also: name().

bool QFile::exists ( const char * fileName ) [static]

Returns TRUE if the file given by fileName exists, otherwise FALSE.

void QFile::flush () [virtual]

Flushes the file buffer to the disk.

close() also flushes the file buffer.

Reimplemented from QIODevice.

int QFile::getch () [virtual]

Reads a single byte/character from the file.

Returns the byte/character read, or -1 if the end of the file has been reached.

See also: putch() and ungetch().

Reimplemented from QIODevice.

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(), as well as with QSocketNotifier.

If the file is not open or there is an error, handle() returns -1.

See also: QSocketNotifier.

const char * QFile::name () const

Returns the name set by setName().

See also: setName() and QFileInfo::fileName().

bool QFile::open ( int m ) [virtual]

Opens the file specified by the file name currently set, using the mode m. Returns TRUE if successful, otherwise FALSE.

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 the data is really written.

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.

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: application/application.cpp

Reimplemented from QIODevice.

bool QFile::open ( int m, FILE * f )

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, strlen(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().

bool QFile::open ( int m, int f )

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.

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().

int QFile::putch ( int ch ) [virtual]

Writes the character ch to the file.

Returns ch, or -1 if some error occurred.

See also: getch() and ungetch().

Reimplemented from QIODevice.

int QFile::readBlock ( char * p, uint 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.

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().

Reimplemented from QIODevice.

int QFile::readLine ( char * p, uint maxlen ) [virtual]

Reads a line of text.

Reads bytes from the file until end-of-line is reached, or up to maxlen bytes, and returns the number of bytes read, or -1 in case of 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.

bool QFile::remove ()

Removes the file specified by the file name currently set. Returns TRUE if successful, otherwise FALSE.

The file is closed before it is removed.

bool QFile::remove ( const char * fileName ) [static]

Removes the file fileName. Returns TRUE if successful, otherwise FALSE.

void QFile::setName ( const char * name )

Sets the name of the file. The name can include an absolute directory path or it can be a name or a path relative to the current directory.

Do not call this function if the file has already been opened.

Note that if the name is relative QFile does not associate it with the current directory. If you change directory before calling open(), open uses the new current directory.

Example:

     QFile f;
     QDir::setCurrent( "/tmp" );
     f.setName( "readme.txt" );
     QDir::setCurrent( "/home" );
     f.open( IO_ReadOnly );        // opens "/home/readme.txt" under UNIX

Also note that the directory separator '/' works for all operating systems supported by Qt.

See also: name(), QFileInfo and QDir.

uint QFile::size () const [virtual]

Returns the file size.

See also: at().

Reimplemented from QIODevice.

int QFile::ungetch ( int ch ) [virtual]

Puts the character ch back into the file and decrements the index if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns ch, or -1 if some error occurred.

See also: getch() and putch().

Reimplemented from QIODevice.

int QFile::writeBlock ( const char * p, uint 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.

Important: When working with buffered files, data may not be written to the file at once. Call flush to make sure the data is really written.

See also: readBlock().

Reimplemented from QIODevice.


Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):


This file is part of the Qt toolkit, copyright © 1995-98 Troll Tech, all rights reserved.

It was generated from the following files:


Copyright © 1998 Troll TechTrademarks
Qt version 1.42