QFile

Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index Return to Main Contents
 

NAME

QFile - I/O device that operates on files  

SYNOPSIS

Almost all the functions in this class are reentrant when Qt is built with thread support. The exceptions are setEncodingFunction(), setDecodingFunction(), and setErrorString(). </p>

#include <qfile.h>

Inherits QIODevice.

 

Public Members


QFile ()

QFile ( const QString & name )

~QFile ()

QString name () const

void setName ( const QString & name )

typedef QCString (* EncoderFn ) ( const QString & fileName )

typedef QString (* DecoderFn ) ( const QCString & localfileName )

bool exists () const

bool remove ()

virtual bool open ( int m )

bool open ( int m, FILE * f )

bool open ( int m, int f )

virtual void close ()

virtual void flush ()

virtual Offset size () const

virtual bool atEnd () const

virtual Q_LONG readLine ( char * p, Q_ULONG maxlen )

Q_LONG readLine ( QString & s, Q_ULONG maxlen )

virtual int getch ()

virtual int putch ( int ch )

virtual int ungetch ( int ch )

int handle () const

QString errorString () const
 

Static Public Members


QCString encodeName ( const QString & fileName )

QString decodeName ( const QCString & localFileName )

void setEncodingFunction ( EncoderFn f )

void setDecodingFunction ( DecoderFn f )

bool exists ( const QString & fileName )

bool remove ( const QString & fileName )
 

Important Inherited Members


virtual QByteArray readAll ()
 

Protected Members


void setErrorString ( const QString & str )
 

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 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

 

QFile::DecoderFn

This is used by QFile::setDecodingFunction().  

QFile::EncoderFn

This is used by QFile::setEncodingFunction().  

MEMBER FUNCTION DOCUMENTATION

 

QFile::QFile ()

Constructs a QFile with no name.  

QFile::QFile ( const QString & name )

Constructs a QFile with a file name name.

See also setName().  

QFile::~QFile ()

Destroys a QFile. Calls close().  

bool QFile::atEnd () const [virtual]

Returns TRUE if the end of file has been reached; otherwise returns FALSE. If QFile has not been open()'d, then the behavior is undefined.

See also size().

Example: distributor/distributor.ui.h.

Reimplemented from QIODevice.  

void QFile::close () [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.

See also open() and flush().

Examples:

Reimplemented from QIODevice.  

QString QFile::decodeName ( const QCString & localFileName ) [static]

This does the reverse of QFile::encodeName() using localFileName.

See also setDecodingFunction().

Example: distributor/distributor.ui.h.  

QCString QFile::encodeName ( const QString & fileName ) [static]

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.

See also decodeName().

Example: distributor/distributor.ui.h.