#include </home/clem/local/src/opie/qmake/include/qfile.h>
Inheritance diagram for QFile:


Public Types | |
| typedef QCString(* | EncoderFn )(const QString &fileName) |
| typedef QString(* | DecoderFn )(const QCString &localfileName) |
Public Member Functions | |
| 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 |
Static Public Member Functions | |
| 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) |
Protected Attributes | |
| QString | fn |
| FILE * | fh |
| int | fd |
| Offset | length |
| bool | ext_f |
| void * | d |
Private Member Functions | |
| void | init () |
| QFile (const QFile &) | |
| QFile & | operator= (const QFile &) |
Private Attributes | |
| QCString | ungetchBuffer |
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.
Definition at line 50 of file qfile.h.
|
|
This is used by QFile::setDecodingFunction(). |
|
|
This is used by QFile::setEncodingFunction(). |
|
|
Constructs a QFile with no name. Definition at line 138 of file qfile.cpp. References init(). |
|
|
Constructs a QFile with a file name name.
Definition at line 149 of file qfile.cpp. References init(). |
|
|
Destroys a QFile. Calls close(). Definition at line 160 of file qfile.cpp. References close(). |
|
|
|
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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
Reimplemented from QIODevice. Definition at line 478 of file qfile_unix.cpp. References FALSE, fd, fh, QIODevice::ioIndex, QIODevice::isOpen(), QIODevice::isRaw(), QIODevice::isSequentialAccess(), l, and qWarning(). |
|
|
Reimplemented from QIODevice. Definition at line 120 of file qfile.h. References QIODevice::ioIndex. Referenced by atEnd(), Gutenbrowser::BackBtn(), Gutenbrowser::BeginBtn(), Gutenbrowser::Bookmark(), Opie::Core::Internal::DebugBackend::debugFile(), Gutenbrowser::ForwardBtn(), WavPluginData::initialise(), Gutenbrowser::load(), Opie::MM::ExifData::ReadJpegSections(), Gutenbrowser::setBookmark(), Gutenbrowser::TopBtn(), ungetch(), and WAVsoundDuration(). |
|
|
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.
Reimplemented from QIODevice. Definition at line 298 of file qfile.cpp. References at(), QIODevice::atEnd(), FALSE, QIODevice::isDirectAccess(), QIODevice::isOpen(), QIODevice::isTranslated(), length, and qWarning(). Referenced by ConfigDlg::addMap(), Gutenbrowser::BeginBtn(), Gutenbrowser::Bookmark(), ConfigDlg::ConfigDlg(), FileManager::copyFile(), Konsole::initCommandList(), Multikey::message(), QMyDialog::QMyDialog(), SysFileMonitor::timerEvent(), and WellenreiterMainWindow::uploadSession(). |
|
|
|
This does the reverse of QFile::encodeName() using localFileName.
Definition at line 591 of file qfile.cpp. References decoder. Referenced by QDir::canonicalPath(), QDir::currentDirPath(), CardMonitor::getStatusSd(), QFileInfo::group(), QDir::homeDirPath(), QFileInfo::owner(), QDir::readDirEntries(), and QFileInfo::readLink(). |
|
|
|
Returns TRUE if the file given by fileName exists; otherwise returns FALSE. Definition at line 245 of file qfile.cpp. References qt_file_access(). |
|
|
|
Flushes the file buffer to the disk. close() also flushes the file buffer. Implements QIODevice. Definition at line 281 of file qfile.cpp. References fh, and QIODevice::isOpen(). Referenced by QMyDialog::SaveClicked(), DCCTransferRecv::slotProcess(), and QCopEnvelope::~QCopEnvelope(). |
|
|
Reads a single byte/character from the file. Returns the byte/character read, or -1 if the end of the file has been reached.
Implements QIODevice. Definition at line 401 of file qfile.cpp. References buf, fh, IO_ReadError, QIODevice::ioIndex, QCString::isEmpty(), QIODevice::isOpen(), QIODevice::isRaw(), QIODevice::isReadable(), QIODevice::isSequentialAccess(), len, QCString::length(), qWarning(), readBlock(), QIODevice::setStatus(), QCString::truncate(), and ungetchBuffer. Referenced by open(), Config::read(), and Opie::MM::ExifData::ReadJpegSections(). |
|
|
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.
Definition at line 644 of file qfile_unix.cpp. References fd, fh, and QIODevice::isOpen(). Referenced by AdvancedFm::copyFile(), MemFile::data(), AppLauncher::execute(), Opie::Core::Internal::Zaurus::hingeSensorTriggered(), Opie::Core::Internal::Zaurus::initHingeSensor(), Gutenbrowser::load(), WavFile::newFile(), WavFile::openFile(), VMemo::openWAV(), QPEApplication::QPEApplication(), AppLauncher::received(), Opie::Core::OGlobal::truncateFile(), WavFile::wavHandle(), and QCopEnvelope::~QCopEnvelope(). |
|
|
For internal use only. Initialize internal data. Definition at line 171 of file qfile.cpp. References ext_f, FALSE, fd, fh, IO_Direct, IO_Ok, QIODevice::ioIndex, length, QIODevice::setFlags(), and QIODevice::setStatus(). |
|
|
||||||||||||
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.
Definition at line 391 of file qfile_unix.cpp. References ext_f, FALSE, fd, getch(), init(), IO_Open, IO_Raw, IO_Sequential, QIODevice::ioIndex, QIODevice::isOpen(), QIODevice::isReadable(), length, qWarning(), QIODevice::resetStatus(), QIODevice::setMode(), QIODevice::setState(), QIODevice::setType(), TRUE, and ungetch(). |
|
||||||||||||
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.
Definition at line 329 of file qfile_unix.cpp. References ext_f, FALSE, fh, QIODevice::flags(), getch(), init(), IO_Open, IO_Raw, IO_Sequential, IO_Truncate, QIODevice::ioIndex, QIODevice::isOpen(), QIODevice::isReadable(), length, qWarning(), QIODevice::setMode(), QIODevice::setState(), QIODevice::setType(), TRUE, and ungetch(). |
|
|
|
|
|
|
Writes the character ch to the file. Returns ch, or -1 if some error occurred.
Implements QIODevice. Definition at line 444 of file qfile.cpp. References buf, fh, IO_WriteError, QIODevice::ioIndex, QIODevice::isOpen(), QIODevice::isRaw(), QIODevice::isSequentialAccess(), QIODevice::isWritable(), length, qWarning(), QIODevice::setStatus(), and writeBlock(). Referenced by IrdaApplet::setIrdaDiscoveryStatus(). |
|
||||||||||||
|
||||||||||||
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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 Note that the string is read as plain Latin1 bytes, not Unicode.
Definition at line 380 of file qfile.cpp. References l, and readLine(). |
|
||||||||||||
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Removes the file fileName. Returns TRUE if successful, otherwise FALSE. Definition at line 70 of file qfile_unix.cpp. References encodeName(), FALSE, QString::isEmpty(), and qWarning(). |
|
|
|
Sets the function for decoding 8-bit file names to f. The default uses the locale-specific 8-bit encoding.
Definition at line 611 of file qfile.cpp. References decoder. |
|
|
Sets the function for encoding Unicode file names to f. The default encodes in the locale-specific 8-bit encoding.
Definition at line 573 of file qfile.cpp. References encoder. |
|
|
|
Returns the file size.
Implements QIODevice. Definition at line 435 of file qfile_unix.cpp. References encodeName(), fd, fh, fn, and QIODevice::isOpen(). Referenced by MemFile::data(), Opie::Core::Internal::DebugBackend::debugFile(), WeatherPluginWidget::displayWeather(), EmailHandler::encodeFile(), FileManager::loadFile(), CameraMainWindow::postProcessVideo(), Score::read(), read_comment(), Opie::MM::ExifData::ReadJpegSections(), Opie::Core::OGlobal::truncateFile(), and WellenreiterMainWindow::uploadSession(). |
|
|
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 an error occurred.
Implements QIODevice. Definition at line 484 of file qfile.cpp. References at(), buf, fh, IO_ReadError, QIODevice::ioIndex, QIODevice::isOpen(), QIODevice::isRaw(), QIODevice::isReadable(), QIODevice::isSequentialAccess(), qWarning(), QIODevice::setStatus(), ungetchBuffer, and writeBlock(). Referenced by open(), and Config::read(). |
|
|
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Reimplemented from QIODevice. Definition at line 86 of file qfile.h. References QIODevice::writeBlock(). |
|
||||||||||||
|
|
|
|
|
|
|
|
Definition at line 100 of file qfile.h. Referenced by at(), close(), handle(), init(), open(), readBlock(), size(), and writeBlock(). |
|
|
Definition at line 99 of file qfile.h. Referenced by at(), close(), flush(), getch(), handle(), init(), open(), putch(), readBlock(), readLine(), Opie::OPimTodoAccessVCal::save(), Opie::OPimContactAccessBackend_VCard::save(), size(), ungetch(), and writeBlock(). |
|
|
Definition at line 98 of file qfile.h. Referenced by exists(), name(), open(), remove(), setName(), and size(). |
|
|
Definition at line 101 of file qfile.h. Referenced by atEnd(), init(), open(), putch(), and writeBlock(). |
|
|
Definition at line 107 of file qfile.h. Referenced by getch(), readBlock(), and ungetch(). |
1.4.2