#include <qfile.h>
Inheritance diagram for QFile:


QFile is an I/O device for reading and writing text and binary files and {The Qt Resource System}{resources}. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream.
The file name is usually passed in the constructor, but it can be set at any time using setFileName(). You can check for a file's existence using exists(), and remove a file using remove(). (More advanced file system related operations are provided by QFileInfo and QDir.)
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 also call the QIODevice-inherited functions read(), readLine(), readAll(), write(). QFile also inherits getChar(), putChar(), and ungetChar(), which work one character at a time.
The size of the file is returned by size(). You can get the current file position using pos(), or move to a new file position using seek(). If you've reached the end of the file, atEnd() returns true.
The following example reads a text file line by line:
snippets/file/file.cpp noStream_snippet QFile /^/
The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("\\r\\n") into C++-style terminators ("\\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.
The next example uses QTextStream to read a text file line by line:
readTextStream_snippet QFile /^/
QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit Unicode QString. By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; see QTextCodec::codecForLocale() for details). This can be changed using setCodec().
To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right:
writeTextStream_snippet QFile /^/
QDataStream is similar, in that you can use operator<<() to write data and operator>>() to read it back. See the class documentation for details.
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 use standard C++ APIs ( <cstdio> or <iostream>) or platform-specific APIs to access files instead of QFile, you can use the encodeName() and decodeName() functions to convert between Unicode file names and 8-bit file names.
On Unix, there are some special system files (e.g. in /proc) for which size() will always return 0, yet you may still be able to read more data from such a file; the data is generated in direct response to you calling read(). In this case, however, you cannot use atEnd() to determine if there is more data to read (since atEnd() will return true for a file that claims to have size 0). Instead, you should either call readAll(), or call read() or readLine() repeatedly until no more data can be read. The next example uses QTextStream to read /proc/modules line by line:
readRegularEmptyFile_snippet QFile /^/
Definition at line 42 of file qfile.h.
Public Types | |
| enum | FileError |
| enum | Permission |
| typedef QByteArray(*) | EncoderFn (const QString &fileName) |
| typedef QString(*) | DecoderFn (const QByteArray &localfileName) |
Public Member Functions | |
| QFile () | |
| QFile (const QString &name) | |
| QFile (QObject *parent) | |
| QFile (const QString &name, QObject *parent) | |
| ~QFile () | |
| FileError | error () const |
| void | unsetError () |
| QString | fileName () const |
| void | setFileName (const QString &name) |
| bool | exists () const |
| QString | readLink () const |
| QString | symLinkTarget () const |
| bool | remove () |
| bool | rename (const QString &newName) |
| bool | link (const QString &newName) |
| bool | copy (const QString &newName) |
| bool | isSequential () const |
| bool | open (OpenMode flags) |
| bool | open (FILE *f, OpenMode flags) |
| bool | open (int fd, OpenMode flags) |
| virtual void | close () |
| qint64 | size () const |
| qint64 | pos () const |
| bool | seek (qint64 offset) |
| bool | atEnd () const |
| bool | flush () |
| bool | resize (qint64 sz) |
| Permissions | permissions () const |
| bool | setPermissions (Permissions permissionSpec) |
| int | handle () const |
| virtual QAbstractFileEngine * | fileEngine () const |
Static Public Member Functions | |
| static QByteArray | encodeName (const QString &fileName) |
| static QString | decodeName (const QByteArray &localFileName) |
| static QString | decodeName (const char *localFileName) |
| static void | setEncodingFunction (EncoderFn) |
| static void | setDecodingFunction (DecoderFn) |
| static bool | exists (const QString &fileName) |
| static QString | readLink (const QString &fileName) |
| static QString | symLinkTarget (const QString &fileName) |
| static bool | remove (const QString &fileName) |
| static bool | rename (const QString &oldName, const QString &newName) |
| static bool | link (const QString &oldname, const QString &newName) |
| static bool | copy (const QString &fileName, const QString &newName) |
| static bool | resize (const QString &filename, qint64 sz) |
| static Permissions | permissions (const QString &filename) |
| static bool | setPermissions (const QString &filename, Permissions permissionSpec) |
Protected Member Functions | |
| QFile (QFilePrivate &dd, QObject *parent=0) | |
| qint64 | readData (char *data, qint64 maxlen) |
| qint64 | writeData (const char *data, qint64 len) |
| qint64 | readLineData (char *data, qint64 maxlen) |
This is a typedef for a pointer to a function with the following signature:
QByteArray myEncoderFunc(const QString &fileName);
This is a typedef for a pointer to a function with the following signature:
QString myDecoderFunc(const QByteArray &localFileName);
| enum QFile::FileError |
This enum describes the errors that may be returned by the error() function.
NoError No error occurred. ReadError An error occurred when reading from the file. WriteError An error occurred when writing to the file. FatalError A fatal error occurred. ResourceError OpenError The file could not be opened. AbortError The operation was aborted. TimeOutError A timeout occurred. UnspecifiedError An unspecified error occurred. RemoveError The file could not be removed. RenameError The file could not be renamed. PositionError The position in the file could not be changed. ResizeError The file could not be resized. PermissionsError The file could not be accessed. CopyError The file could not be copied.
ConnectError
Definition at line 51 of file qfile.h.
00051 { 00052 NoError = 0, 00053 ReadError = 1, 00054 WriteError = 2, 00055 FatalError = 3, 00056 ResourceError = 4, 00057 OpenError = 5, 00058 AbortError = 6, 00059 TimeOutError = 7, 00060 UnspecifiedError = 8, 00061 RemoveError = 9, 00062 RenameError = 10, 00063 PositionError = 11, 00064 ResizeError = 12, 00065 PermissionsError = 13, 00066 CopyError = 14 00067 #ifdef QT3_SUPPORT 00068 , ConnectError = 30 00069 #endif 00070 };
| enum QFile::Permission |
This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.
ReadOwner The file is readable by the owner of the file. WriteOwner The file is writable by the owner of the file. ExeOwner The file is executable by the owner of the file. ReadUser The file is readable by the user. WriteUser The file is writable by the user. ExeUser The file is executable by the user. ReadGroup The file is readable by the group. WriteGroup The file is writable by the group. ExeGroup The file is executable by the group. ReadOther The file is readable by anyone. WriteOther The file is writable by anyone. ExeOther The file is executable by anyone.
Definition at line 72 of file qfile.h.
00072 { 00073 ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000, 00074 ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100, 00075 ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010, 00076 ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001 00077 };
| QFile::QFile | ( | ) |
Definition at line 289 of file qfile.cpp.
Referenced by copy(), link(), permissions(), remove(), rename(), resize(), and setPermissions().
00290 : QIODevice(*new QFilePrivate, 0) 00291 { 00292 }
| QFile::QFile | ( | const QString & | name | ) |
| QFile::QFile | ( | QObject * | parent | ) | [explicit] |
Constructs a new file object with the given parent.
Definition at line 296 of file qfile.cpp.
00297 : QIODevice(*new QFilePrivate, parent) 00298 { 00299 }
| QFile::~QFile | ( | ) |
| QFile::QFile | ( | QFilePrivate & | dd, | |
| QObject * | parent = 0 | |||
| ) | [protected] |
| QFile::FileError QFile::error | ( | ) | const |
Returns the file error status.
The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
Definition at line 1285 of file qfile.cpp.
References d.
Referenced by close(), copy(), fontPath(), main(), QAnimationWriter::okay(), remove(), and rename().
| void QFile::unsetError | ( | ) |
| QString QFile::fileName | ( | ) | const |
Returns the name set by setFileName().
Definition at line 344 of file qfile.cpp.
References QAbstractFileEngine::DefaultName, fileEngine(), and QAbstractFileEngine::fileName().
Referenced by copy(), encodeName(), exists(), QImageReader::fileName(), QImageWriter::fileName(), FtpWindow::ftpCommandFinished(), FileManager::generateFiles(), QImageReaderPrivate::initHandler(), link(), loadFile(), permissions(), QFileInfo::QFileInfo(), QIODevice::QIODevice(), QDesignerActions::readInForm(), readLink(), remove(), rename(), resize(), QFileInfo::setFile(), setPermissions(), QConfFileSettingsPrivate::syncConfFile(), and QDesignerActions::writeOutForm().
00345 { 00346 return fileEngine()->fileName(QAbstractFileEngine::DefaultName); 00347 }
Here is the call graph for this function:

| void QFile::setFileName | ( | const QString & | name | ) |
Sets the name of the file. The name can have no path, a relative path, or an 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 the application's current directory path {at the time of the open()} call.
Example:
QFile file; QDir::setCurrent("/tmp"); file.setFileName("readme.txt"); QDir::setCurrent("/home"); file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix
Note that the directory separator "/" works for all operating systems supported by Qt.
Definition at line 374 of file qfile.cpp.
References close(), d, QIODevice::isOpen(), name, and qWarning().
Referenced by fontPath(), QImageReaderPrivate::initHandler(), main(), openFile(), Uic::printDependencies(), QPictureIO::read(), Driver::uic(), QPictureIO::write(), and QDesignerActions::writeOutForm().
00375 { 00376 Q_D(QFile); 00377 if (isOpen()) { 00378 qWarning("QFile::setFileName: File is already opened"); 00379 close(); 00380 } 00381 if(d->fileEngine) { //get a new file engine later 00382 delete d->fileEngine; 00383 d->fileEngine = 0; 00384 } 00385 d->fileName = name; 00386 }
Here is the call graph for this function:

| QByteArray QFile::encodeName | ( | const QString & | fileName | ) | [static] |
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.
Definition at line 407 of file qfile.cpp.
References QFilePrivate::encoder, and fileName().
Referenced by QInotifyFileSystemWatcherEngine::addPaths(), QKqueueFileSystemWatcherEngine::addPaths(), combinePath(), QProcessPrivate::createChannel(), QFSFileEnginePrivate::doStat(), QFSFileEngine::entryList(), QProcessPrivate::execChild(), QFSFileEngine::fileFlags(), QFSFileEngine::fileName(), QFileInfoPrivate::hasAccess(), QLibraryPrivate::isPlugin(), QFSFileEnginePrivate::isSymlink(), QFSFileEngine::link(), QTranslator::load(), QLibraryPrivate::load_sys(), QFSFileEngine::mkdir(), QSQLite2Driver::open(), QFSFileEngine::open(), qt_unix_query(), registerFont(), QDynamicResourceRoot::registerSelf(), QFSFileEngine::remove(), QFSFileEngine::rename(), QFSFileEngine::rmdir(), QFSFileEngine::setCurrentPath(), QPluginLoader::setFileName(), QFSFileEngine::setPermissions(), Q3TextDocument::setRichTextInternal(), QFSFileEngine::setSize(), QFSFileEngine::size(), QProcessPrivate::startDetached(), QProcessPrivate::startProcess(), and QFSFileEnginePrivate::sysOpen().
00408 { 00409 return (*QFilePrivate::encoder)(fileName); 00410 }
Here is the call graph for this function:

| QString QFile::decodeName | ( | const QByteArray & | localFileName | ) | [static] |
This does the reverse of QFile::encodeName() using localFileName.
Definition at line 432 of file qfile.cpp.
References QFilePrivate::decoder.
Referenced by QCoreApplication::applicationFilePath(), combinePath(), QFSFileEngine::currentPath(), QFSFileEngine::entryList(), QFSFileEngine::fileName(), QFSFileEngine::homePath(), QDesigner::initialize(), main(), qdesigner_internal::QrcView::mimeFileList(), QFSFileEngine::owner(), and QFSFileEngine::tempPath().
00433 { 00434 return (*QFilePrivate::decoder)(localFileName); 00435 }
| QString QFile::decodeName | ( | const char * | localFileName | ) | [inline, static] |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns the Unicode version of the given localFileName. See encodeName() for details.
Definition at line 98 of file qfile.h.
00099 { return decodeName(QByteArray(localFileName)); };
| void QFile::setEncodingFunction | ( | EncoderFn | function | ) | [static] |
Sets the function for encoding Unicode file names. The default encodes in the locale-specific 8-bit encoding.
Definition at line 449 of file qfile.cpp.
References QFilePrivate::encoder, and locale_encode().
00450 { 00451 if (!f) 00452 f = locale_encode; 00453 QFilePrivate::encoder = f; 00454 }
Here is the call graph for this function:

| void QFile::setDecodingFunction | ( | DecoderFn | function | ) | [static] |
Sets the function for decoding 8-bit file names. The default uses the locale-specific 8-bit encoding.
Definition at line 481 of file qfile.cpp.
References QFilePrivate::decoder, and locale_decode().
00482 { 00483 if (!f) 00484 f = locale_decode; 00485 QFilePrivate::decoder = f; 00486 }
Here is the call graph for this function:

| bool QFile::exists | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns true if the file specified by fileName() exists; otherwise returns false.
Definition at line 498 of file qfile.cpp.
References QAbstractFileEngine::ExistsFlag, fileEngine(), and QAbstractFileEngine::FlagsMask.
Referenced by SaveFormAsTemplate::accept(), IncludeFiles::angleBracketLookup(), QCoreApplicationPrivate::appendApplicationPathToLibraryPaths(), QCoreApplication::applicationFilePath(), Config::applicationIcon(), Ui3Reader::computeDeps(), Ui3Reader::createFormImpl(), qdesigner_internal::createIconSet(), qdesigner_internal::QDesignerResource::createResources(), AnimationSaveWidget::detectPpmtoMpegCommand(), DisplayLock::DisplayLock(), Config::docIcon(), Q3FileDialog::done(), FtpWindow::downloadFile(), HttpWindow::downloadFile(), TorrentView::dropEvent(), ProjectPorter::enableAttributes(), PreprocessorController::evaluate(), ProFileEvaluator::evaluateConditionalFunction(), QDir::exists(), QLibraryInfoPrivate::findConfiguration(), findRulesFile(), fontPath(), FileManager::generateFiles(), IncludeFiles::IncludeFiles(), QConfFileSettingsPrivate::isWritable(), TextEdit::load(), Config::loadConfig(), main(), TrWindow::newPhraseBook(), MainWindow::on_actionAboutApplication_triggered(), openModeToFopenMode(), ProjectPorter::portProject(), Q_GLOBAL_STATIC_WITH_ARGS(), qPixmapFromMimeSource(), IncludeFiles::quoteLookup(), PreprocessorCache::readFile(), PreprocessorController::readFile(), QDesignerClient::readFromSocket(), Porting::readXML(), QDynamicResourceRoot::registerSelf(), HelpDialog::removeOldCacheFiles(), rename(), QDir::rename(), IncludeFiles::resolve(), PortingRules::resolveFileName(), IncludeFiles::searchIncludePaths(), HelpDialog::setupFullTextIndex(), HelpDialog::setupTitleMap(), qdesigner_internal::FindIconDialog::setViewDir(), QDesignerActions::showHelp(), Q3Process::start(), QConfFileSettingsPrivate::syncConfFile(), qdesigner_internal::ResourceEditor::updateQrcStack(), and FileWriter::writeFile().
00499 { 00500 // 0x1000000 = QAbstractFileEngine::Refresh, forcing an update 00501 return (fileEngine()->fileFlags(QAbstractFileEngine::FlagsMask 00502 | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag); 00503 }
Here is the call graph for this function:

| bool QFile::exists | ( | const QString & | fileName | ) | [static] |
Returns true if the file specified by fileName exists; otherwise returns false.
Definition at line 511 of file qfile.cpp.
References fileName().
Here is the call graph for this function:

| QString QFile::readLink | ( | ) | const |
Use symLinkTarget() instead.
Definition at line 537 of file qfile.cpp.
References fileEngine(), QAbstractFileEngine::fileName(), and QAbstractFileEngine::LinkName.
00538 { 00539 return fileEngine()->fileName(QAbstractFileEngine::LinkName); 00540 }
Here is the call graph for this function:

Use symLinkTarget() instead.
Definition at line 560 of file qfile.cpp.
References fileName().
Here is the call graph for this function:

| QString QFile::symLinkTarget | ( | ) | const [inline] |
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
Definition at line 108 of file qfile.h.
00108 { return readLink(); }
This name may not represent an existing file; it is only a string. QFile::exists() returns true if the symlink points to an existing file.
Definition at line 109 of file qfile.h.
| bool QFile::remove | ( | ) |
Removes the file specified by fileName(). Returns true if successful; otherwise returns false.
The file is closed before it is removed.
Definition at line 575 of file qfile.cpp.
References close(), d, error(), fileEngine(), NoError, qWarning(), RemoveError, and unsetError().
Referenced by SaveFormAsTemplate::accept(), QDirModel::dropMimeData(), FtpWindow::ftpCommandFinished(), HttpWindow::httpRequestFinished(), QDir::remove(), removeBackup(), HelpDialog::removeOldCacheFiles(), AnimationSaveWidget::removeTemporaryFiles(), rename(), AnimationSaveWidget::save(), and QTemporaryFile::~QTemporaryFile().
00576 { 00577 Q_D(QFile); 00578 if (d->fileName.isEmpty()) { 00579 qWarning("QFile::remove: Empty or null file name"); 00580 return false; 00581 } 00582 close(); 00583 if(error() == QFile::NoError) { 00584 if(fileEngine()->remove()) { 00585 unsetError(); 00586 return true; 00587 } 00588 d->setError(QFile::RemoveError, errno); 00589 } 00590 return false; 00591 }
Here is the call graph for this function:

| bool QFile::remove | ( | const QString & | fileName | ) | [static] |
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 specified by the fileName given.
Returns true if successful; otherwise returns false.
Definition at line 604 of file qfile.cpp.
References fileName(), and QFile().
Here is the call graph for this function:

| bool QFile::rename | ( | const QString & | newName | ) |
Renames the file currently specified by fileName() to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).
The file is closed before it is renamed.
Definition at line 622 of file qfile.cpp.
References atEnd(), close(), d, error(), QIODevice::errorString(), exists(), fileEngine(), fileName(), QIODevice::isOpen(), NoError, open(), QFile(), qWarning(), QIODevice::read(), QIODevice::ReadOnly, remove(), RenameError, QIODevice::Truncate, unsetError(), QIODevice::write(), and QIODevice::WriteOnly.
Referenced by copy(), QDir::rename(), and AnimationSaveWidget::save().
00623 { 00624 Q_D(QFile); 00625 if (d->fileName.isEmpty()) { 00626 qWarning("QFile::rename: Empty or null file name"); 00627 return false; 00628 } 00629 if (QFile(newName).exists()) { 00630 // ### Race condition. If a file is moved in after this, it /will/ be 00631 // overwritten. On Unix, the proper solution is to use hardlinks: 00632 // return ::link(old, new) && ::remove(old); 00633 d->setError(QFile::RenameError, QLatin1String("Destination file exists")); 00634 return false; 00635 } 00636 close(); 00637 if(error() == QFile::NoError) { 00638 if (fileEngine()->rename(newName)) { 00639 unsetError(); 00640 return true; 00641 } 00642 00643 QFile in(fileName()); 00644 QFile out(newName); 00645 if (in.open(QIODevice::ReadOnly)) { 00646 if(out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { 00647 bool error = false; 00648 char block[1024]; 00649 while (!in.atEnd()) { 00650 qint64 read = in.read(block, 1024); 00651 if (read == -1) { 00652 d->setError(QFile::RenameError, in.errorString()); 00653 error = true; 00654 break; 00655 } 00656 if (read != out.write(block, read)) { 00657 d->setError(QFile::RenameError, out.errorString()); 00658 error = true; 00659 break; 00660 } 00661 } 00662 if(!error) 00663 in.remove(); 00664 return !error; 00665 } 00666 } 00667 d->setError(QFile::RenameError, out.isOpen() ? in.errorString() : out.errorString()); 00668 } 00669 return false; 00670 }
Here is the call graph for this function:

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Renames the file oldName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).
Definition at line 685 of file qfile.cpp.
References QFile().
00686 { 00687 return QFile(oldName).rename(newName); 00688 }
Here is the call graph for this function:

| bool QFile::link | ( | const QString & | linkName | ) |
Creates a link named linkName that points to the file currently specified by fileName(). What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
Definition at line 700 of file qfile.cpp.
References QFileInfo::absoluteFilePath(), d, fileEngine(), qWarning(), RenameError, and unsetError().
Referenced by QDirModel::dropMimeData().
00701 { 00702 Q_D(QFile); 00703 if (d->fileName.isEmpty()) { 00704 qWarning("QFile::link: Empty or null file name"); 00705 return false; 00706 } 00707 QFileInfo fi(linkName); 00708 if(fileEngine()->link(fi.absoluteFilePath())) { 00709 unsetError(); 00710 return true; 00711 } 00712 d->setError(QFile::RenameError, errno); 00713 return false; 00714 }
Here is the call graph for this function:

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a link named linkName that points to the file fileName. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
Definition at line 728 of file qfile.cpp.
References fileName(), and QFile().
Here is the call graph for this function:

| bool QFile::copy | ( | const QString & | newName | ) |
Copies the file currently specified by fileName() to a file called newName. Returns true if successful; otherwise returns false.
Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
The source file is closed before it is copied.
Definition at line 746 of file qfile.cpp.
References QString::arg(), atEnd(), close(), CopyError, d, error(), fileEngine(), NoError, QTemporaryFile::open(), open(), permissions(), qWarning(), QIODevice::read(), QIODevice::ReadOnly, rename(), setPermissions(), unsetError(), and QIODevice::write().
Referenced by createBackup(), QDirModel::dropMimeData(), MainWindow::on_actionSaveAs_triggered(), and Skin::setupDefaultButtons().
00747 { 00748 Q_D(QFile); 00749 if (d->fileName.isEmpty()) { 00750 qWarning("QFile::copy: Empty or null file name"); 00751 return false; 00752 } 00753 close(); 00754 if(error() == QFile::NoError) { 00755 if(fileEngine()->copy(newName)) { 00756 unsetError(); 00757 return true; 00758 } else { 00759 bool error = false; 00760 if(!open(QFile::ReadOnly)) { 00761 error = true; 00762 QString errorMessage = QLatin1String("Cannot open %1 for input"); 00763 d->setError(QFile::CopyError, errorMessage.arg(d->fileName)); 00764 } else { 00765 QTemporaryFile out; 00766 if(!out.open()) { 00767 close(); 00768 error = true; 00769 d->setError(QFile::CopyError, QLatin1String("Cannot open for output")); 00770 } else { 00771 char block[1024]; 00772 while(!atEnd()) { 00773 qint64 in = read(block, 1024); 00774 if(in == -1) 00775 break; 00776 if(in != out.write(block, in)) { 00777 d->setError(QFile::CopyError, QLatin1String("Failure to write block")); 00778 error = true; 00779 break; 00780 } 00781 } 00782 if(!error && !out.rename(newName)) { 00783 error = true; 00784 QString errorMessage = QLatin1String("Cannot create %1 for output"); 00785 d->setError(QFile::CopyError, errorMessage.arg(newName)); 00786 } 00787 } 00788 } 00789 if(!error) { 00790 QFile::setPermissions(newName, permissions()); 00791 unsetError(); 00792 return true; 00793 } 00794 } 00795 } 00796 return false; 00797 }
Here is the call graph for this function:

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Copies the file fileName to newName. Returns true if successful; otherwise returns false.
If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
Definition at line 812 of file qfile.cpp.
References fileName(), and QFile().
Here is the call graph for this function:

| bool QFile::isSequential | ( | ) | const [virtual] |
Returns true if the file can only be manipulated sequentially; otherwise returns false.
Most files support random-access, but some special files may not.
Reimplemented from QIODevice.
Definition at line 825 of file qfile.cpp.
References d.
Referenced by atEnd().
00826 { 00827 Q_D(const QFile); 00828 return d->fileEngine && d->fileEngine->isSequential(); 00829 }
| bool QFile::open | ( | OpenMode | mode | ) | [virtual] |
Opens the file using OpenMode mode.
The mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
Reimplemented from QIODevice.
Definition at line 840 of file qfile.cpp.
References QIODevice::Append, d, QAbstractFileEngine::error(), QIODevice::errorString(), fileEngine(), QIODevice::isOpen(), OpenError, qWarning(), QIODevice::ReadOnly, seek(), QIODevice::setOpenMode(), size(), unsetError(), UnspecifiedError, and QIODevice::WriteOnly.
Referenced by SaveFormAsTemplate::accept(), QFontDatabase::addApplicationFont(), QGLContext::bindTexture(), HelpDialog::buildContentDict(), HelpDialog::buildKeywordDB(), CalculatorForm::CalculatorForm(), AnimationSaveWidget::convertToMpeg(), copy(), QTemporaryFile::createLocalFile(), DocuParser::createParser(), Q3MimeSourceFactory::dataInternal(), Q3Dns::doResInit(), FtpWindow::downloadFile(), HttpWindow::downloadFile(), QPdfEnginePrivate::embedFont(), fetchtr_ui(), TextEdit::fileSave(), fontFile(), fontPath(), NewForm::formPreviewIcon(), FileManager::generateFiles(), HelpDialog::getAllContents(), Index::getDocumentTitle(), QFreetypeFace::getFace(), getLprPrinters(), QImageReader::imageFormat(), QImageReaderPrivate::initHandler(), HelpDialog::insertBookmarks(), QConfFileSettingsPrivate::isWritable(), TextEdit::load(), qdesigner_internal::WidgetBoxTreeView::load(), PhraseBook::load(), QSvgTinyDocument::load(), QTranslator::load(), Q3Picture::load(), QPicture::load(), MetaTranslator::load(), Main::loadConfig(), ArthurFrame::loadDescription(), Main::loadFeatures(), MainWindow::loadFile(), MdiChild::loadFile(), loadFile(), FormHolder::loadFormFile(), HelpDialog::loadIndexFile(), MainWindow::loadLayout(), QTextBrowser::loadResource(), PortingRules::loadXml(), main(), MainWindow::modelFromFile(), MainWindow::on_actionAboutApplication_triggered(), MainWindow::on_actionSaveAs_triggered(), NewForm::on_buttonBox_clicked(), QTemporaryFile::open(), openFile(), FindFileDialog::openFile(), Q3LocalFs::operationGet(), Q3LocalFs::operationPut(), Index::parseDocument(), parseEtcLpPrinters(), parseNsswitchConf(), parsePrintcap(), parsePrintersConf(), parseQconfig(), QPictureIO::pictureFormat(), ProjectPorter::portProFile(), Preprocessor::preprocess(), Preprocessor::preprocessed(), PreprocessorController::PreprocessorController(), Uic::printDependencies(), QAnimationWriter::QAnimationWriter(), qt_unix_query(), QTextStream::QTextStream(), QPictureIO::read(), ProReader::read(), Index::readDict(), Index::readDocumentList(), PreprocessorCache::readFile(), PreprocessorController::readFile(), QDesignerActions::readInForm(), readInput(), Porting::readXML(), QDynamicResourceRoot::registerSelf(), MetaTranslator::release(), MessageModel::release(), rename(), PhraseBook::save(), qdesigner_internal::WidgetBoxTreeView::save(), QPicture::save(), MetaTranslator::save(), Q3SVGPaintEngine::save(), Translator::save(), HelpDialog::saveBookmarks(), Main::saveConfig(), MdiChild::saveFile(), MainWindow::saveFile(), MainWindow::saveLayout(), Skin::screenSize(), Index::searchForPattern(), Skin::secondaryScreenSize(), qdesigner_internal::BrushManagerProxy::setBrushManager(), TorrentClient::setTorrent(), AddTorrentDialog::setTorrent(), MainWindow::setupBookmarkMenu(), QPrintDialogPrivate::setupPrinter(), TrPreviewTool::showAboutBox(), ArthurFrame::showSource(), Skin::Skin(), Ping::start(), QConfFileSettingsPrivate::syncConfFile(), Driver::uic(), QPictureIO::write(), Index::writeDict(), Index::writeDocumentList(), FileWriter::writeFile(), and QDesignerActions::writeOutForm().
00841 { 00842 Q_D(QFile); 00843 if (isOpen()) { 00844 qWarning("QFile::open: File already open"); 00845 return false; 00846 } 00847 if (mode & Append) 00848 mode |= WriteOnly; 00849 00850 unsetError(); 00851 if ((mode & (ReadOnly | WriteOnly)) == 0) { 00852 qWarning("QIODevice::open: File access not specified"); 00853 return false; 00854 } 00855 if (fileEngine()->open(mode)) { 00856 setOpenMode(mode); 00857 if (mode & Append) 00858 seek(size()); 00859 return true; 00860 } 00861 QFile::FileError err = fileEngine()->error(); 00862 if(err == QFile::UnspecifiedError) 00863 err = QFile::OpenError; 00864 d->setError(err, fileEngine()->errorString()); 00865 return false; 00866 }
Here is the call graph for this function:

| bool QFile::open | ( | FILE * | fh, | |
| OpenMode | mode | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Opens the existing file handle fh in the given mode. Returns true if successful; otherwise returns false.
Example:
#include <stdio.h> void printError(const char* msg) { QFile file; file.open(stderr, QIODevice::WriteOnly); file.write(msg, qstrlen(msg)); // write to stderr file.close(); }
When a QFile is opened using this function, close() does not actually close the file, but only flushes it.
stdin, stdout, or stderr, you may not be able to seek(). See QIODevice::isSequentialAccess() for more information.Definition at line 901 of file qfile.cpp.
References QIODevice::Append, d, QIODevice::isOpen(), pos(), qWarning(), QIODevice::ReadOnly, seek(), QIODevice::setOpenMode(), size(), unsetError(), and QIODevice::WriteOnly.
00902 { 00903 Q_D(QFile); 00904 if (isOpen()) { 00905 qWarning("QFile::open: File already open"); 00906 return false; 00907 } 00908 if (mode & Append) 00909 mode |= WriteOnly; 00910 unsetError(); 00911 if ((mode & (ReadOnly | WriteOnly)) == 0) { 00912 qWarning("QFile::open: File access not specified"); 00913 return false; 00914 } 00915 if(d->openExternalFile(mode, fh)) { 00916 setOpenMode(mode); 00917 if (mode & Append) { 00918 seek(size()); 00919 } else { 00920 long pos = ftell(fh); 00921 if (pos != -1) 00922 seek(pos); 00923 } 00924 return true; 00925 } 00926 return false; 00927 }
Here is the call graph for this function:

| bool QFile::open | ( | int | fd, | |
| OpenMode | mode | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Opens the existing file descripter fd in the given mode. Returns true if successful; otherwise returns 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.
stdin), 1 (stdout), or 2 (stderr), you may not be able to seek(). size() is set to LLONG_MAX (in <climits>).Definition at line 954 of file qfile.cpp.
References QIODevice::Append, d, QIODevice::isOpen(), qWarning(), QIODevice::ReadOnly, seek(), QIODevice::setOpenMode(), size(), unsetError(), and QIODevice::WriteOnly.
00955 { 00956 Q_D(QFile); 00957 if (isOpen()) { 00958 qWarning("QFile::open: File already open"); 00959 return false; 00960 } 00961 if (mode & Append) 00962 mode |= WriteOnly; 00963 unsetError(); 00964 if ((mode & (ReadOnly | WriteOnly)) == 0) { 00965 qWarning("QFile::open: File access not specified"); 00966 return false; 00967 } 00968 if(d->openExternalFile(mode, fd)) { 00969 setOpenMode(mode); 00970 if (mode & Append) 00971 seek(size()); 00972 return true; 00973 } 00974 return false; 00975 }
Here is the call graph for this function:

| void QFile::close | ( | ) | [virtual] |
Reimplemented from QIODevice.
Definition at line 1136 of file qfile.cpp.
References QIODevice::close(), d, error(), QIODevice::errorString(), fileEngine(), QIODevice::isOpen(), and unsetError().
Referenced by SaveFormAsTemplate::accept(), QGLContext::bindTexture(), CalculatorForm::CalculatorForm(), AnimationSaveWidget::convertToMpeg(), copy(), QTemporaryFile::createLocalFile(), QPdfEnginePrivate::embedFont(), fetchtr_ui(), fontPath(), NewForm::formPreviewIcon(), FtpWindow::ftpCommandFinished(), FileManager::generateFiles(), HelpDialog::getAllContents(), HttpWindow::httpRequestFinished(), HelpDialog::loadIndexFile(), QTextBrowser::loadResource(), main(), MainWindow::on_actionSaveAs_triggered(), MainWindow::openFile(), Q3LocalFs::operationGet(), Q3LocalFs::operationPut(), Index::parseDocument(), QPictureIO::pictureFormat(), ProjectPorter::portProFile(), Preprocessor::preprocess(), PreprocessorController::PreprocessorController(), qt_unix_query(), QPictureIO::read(), ProReader::read(), Index::readDict(), MetaTranslator::release(), MessageModel::release(), remove(), rename(), Index::searchForPattern(), qdesigner_internal::BrushManagerProxy::setBrushManager(), setFileName(), Driver::uic(), QPictureIO::write(), QDesignerActions::writeOutForm(), FileManager::~FileManager(), ~QFile(), and QTemporaryFile::~QTemporaryFile().
01137 { 01138 Q_D(QFile); 01139 if(!isOpen()) 01140 return; 01141 QIODevice::close(); 01142 01143 unsetError(); 01144 if(!fileEngine()->close()) 01145 d->setError(fileEngine()->error(), fileEngine()->errorString()); 01146 }
Here is the call graph for this function:

| qint64 QFile::size | ( | ) | const [virtual] |
Returns the size of the file.
For regular empty files on Unix (e.g. those in /proc), this function returns 0; the contents of such a file are generated on demand in response to you calling read().
Reimplemented from QIODevice.
Definition at line 1156 of file qfile.cpp.
References fileEngine(), and QAbstractFileEngine::size().
Referenced by Q3MimeSourceFactory::dataInternal(), FileManager::generateFiles(), QTranslator::load(), open(), Q3LocalFs::operationGet(), qt_unix_query(), and QDynamicResourceRoot::registerSelf().
01157 { 01158 return fileEngine()->size(); 01159 }
Here is the call graph for this function:

| qint64 QFile::pos | ( | ) | const [virtual] |
Reimplemented from QIODevice.
Definition at line 1165 of file qfile.cpp.
References QIODevice::pos().
Referenced by QTemporaryFile::createLocalFile(), open(), and resize().
01166 { 01167 return QIODevice::pos(); 01168 }
Here is the call graph for this function:

| bool QFile::seek | ( | qint64 | off | ) | [virtual] |
Reimplemented from QIODevice.
Definition at line 1191 of file qfile.cpp.
References d, QAbstractFileEngine::error(), QIODevice::errorString(), fileEngine(), QIODevice::isOpen(), NoError, PositionError, qWarning(), QIODevice::seek(), and UnspecifiedError.
Referenced by QGLContext::bindTexture(), QTemporaryFile::createLocalFile(), Index::getCharsetForDocument(), open(), ProjectPorter::portProFile(), QPictureIO::read(), resize(), and QConfFileSettingsPrivate::syncConfFile().
01192 { 01193 Q_D(QFile); 01194 if (!isOpen()) { 01195 qWarning("QFile::seek: IODevice is not open"); 01196 return false; 01197 } 01198 01199 if (!fileEngine()->seek(off) || !QIODevice::seek(off)) { 01200 QFile::FileError err = fileEngine()->error(); 01201 if(err == QFile::UnspecifiedError) 01202 err = QFile::PositionError; 01203 d->setError(err, fileEngine()->errorString()); 01204 return false; 01205 } 01206 d->error = NoError; 01207 return true; 01208 }
Here is the call graph for this function:

| bool QFile::atEnd | ( | ) | const [virtual] |
Returns true if the end of the file has been reached; otherwise returns false.
For regular empty files on Unix (e.g. those in /proc), this function returns true, since the file system reports that the size of such a file is 0. Therefore, you should not depend on atEnd() when reading data from such a file, but rather call read() until no more data can be read.
Reimplemented from QIODevice.
Definition at line 1180 of file qfile.cpp.
References QIODevice::atEnd(), QIODevice::bytesAvailable(), QIODevice::isOpen(), and isSequential().
Referenced by copy(), fontFile(), main(), MainWindow::modelFromFile(), parseEtcLpPrinters(), parseNsswitchConf(), parsePrintcap(), parsePrintersConf(), and rename().
01181 { 01182 if (!isOpen()) 01183 return true; 01184 return QIODevice::atEnd() || (isSequential() && bytesAvailable() == 0); 01185 }
Here is the call graph for this function:

| bool QFile::flush | ( | ) |
Flushes any buffered data to the file. Returns true if successful; otherwise returns false.
Definition at line 1118 of file qfile.cpp.
References d, QAbstractFileEngine::error(), QIODevice::errorString(), fileEngine(), UnspecifiedError, and WriteError.
Referenced by QTextStreamPrivate::flushWriteBuffer(), Q3LocalFs::operationPut(), and registerFont().
01119 { 01120 Q_D(QFile); 01121 if (!fileEngine()->flush()) { 01122 QFile::FileError err = fileEngine()->error(); 01123 if(err == QFile::UnspecifiedError) 01124 err = QFile::WriteError; 01125 d->setError(err, fileEngine()->errorString()); 01126 return false; 01127 } 01128 return true; 01129 }
Here is the call graph for this function:

| bool QFile::resize | ( | qint64 | sz | ) |
Sets the file size (in bytes) sz. Returns true if the file if the resize succeeds; false otherwise. If sz is larger than the file currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
Definition at line 1023 of file qfile.cpp.
References d, fileEngine(), QIODevice::isOpen(), pos(), ResizeError, seek(), and unsetError().
Referenced by FileManager::generateFiles(), QConfFileSettingsPrivate::syncConfFile(), and QDesignerActions::writeOutForm().
01024 { 01025 Q_D(QFile); 01026 if (isOpen() && fileEngine()->pos() > sz) 01027 seek(sz); 01028 if(fileEngine()->setSize(sz)) { 01029 unsetError(); 01030 return true; 01031 } 01032 d->setError(QFile::ResizeError, errno); 01033 return false; 01034 }
Here is the call graph for this function:

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets fileName to size (in bytes) sz. Returns true if the file if the resize succeeds; false otherwise. If sz is larger than fileName currently is the new bytes will be set to 0, if sz is smaller the file is simply truncated.
Definition at line 1048 of file qfile.cpp.
References fileName(), and QFile().
Here is the call graph for this function:

| QFile::Permissions QFile::permissions | ( | ) | const |
Returns the complete OR-ed together combination of QFile::Permission for the file.
Definition at line 1061 of file qfile.cpp.
References fileEngine(), QAbstractFileEngine::fileFlags(), and QAbstractFileEngine::PermsMask.
Referenced by copy().
01062 { 01063 QAbstractFileEngine::FileFlags perms = fileEngine()->fileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask; 01064 return QFile::Permissions((int)perms); //ewww 01065 }
Here is the call graph for this function:

| QFile::Permissions QFile::permissions | ( | const QString & | fileName | ) | [static] |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Returns the complete OR-ed together combination of QFile::Permission for fileName.
Definition at line 1075 of file qfile.cpp.
References fileName(), and QFile().
Here is the call graph for this function:

| bool QFile::setPermissions | ( | Permissions | permissions | ) |
Sets the permissions for the file to the permissions specified. Returns true if successful, or false if the permissions cannot be modified.
Definition at line 1089 of file qfile.cpp.
References d, fileEngine(), PermissionsError, and unsetError().
Referenced by copy(), and QConfFileSettingsPrivate::syncConfFile().
01090 { 01091 Q_D(QFile); 01092 if(fileEngine()->setPermissions(permissions)) { 01093 unsetError(); 01094 return true; 01095 } 01096 d->setError(QFile::PermissionsError, errno); 01097 return false; 01098 }
Here is the call graph for this function:

| bool QFile::setPermissions | ( | const QString & | fileName, | |
| Permissions | permissions | |||
| ) | [static] |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets the permissions for fileName file to permissions.
Definition at line 1107 of file qfile.cpp.
References fileName(), and QFile().
01108 { 01109 return QFile(fileName).setPermissions(permissions); 01110 }
Here is the call graph for this function:

| 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 (i.e. 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 991 of file qfile.cpp.
References fileEngine(), and QIODevice::isOpen().
Referenced by QTextStreamPrivate::fillReadBuffer(), qt_unix_query(), and QConfFileSettingsPrivate::syncConfFile().
00992 { 00993 if (!isOpen()) 00994 return -1; 00995 00996 if (QAbstractFileEngine *engine = fileEngine()) 00997 return engine->handle(); 00998 return -1; 00999 }
Here is the call graph for this function:

| QAbstractFileEngine * QFile::fileEngine | ( | ) | const [virtual] |
Definition at line 1266 of file qfile.cpp.
References QAbstractFileEngine::create(), and d.
Referenced by QFontDatabase::addApplicationFont(), close(), copy(), QTemporaryFile::createLocalFile(), exists(), fileName(), flush(), QFreetypeFace::getFace(), handle(), link(), open(), permissions(), readData(), readLineData(), readLink(), remove(), rename(), resize(), seek(), setPermissions(), size(), and writeData().
01267 { 01268 Q_D(const QFile); 01269 if(!d->fileEngine) 01270 d->fileEngine = QAbstractFileEngine::create(d->fileName); 01271 return d->fileEngine; 01272 }
Here is the call graph for this function:

Implements QIODevice.
Definition at line 1222 of file qfile.cpp.
References d, QAbstractFileEngine::error(), QIODevice::errorString(), fileEngine(), NoError, QIODevice::read(), QAbstractFileEngine::read(), ReadError, and UnspecifiedError.
01223 { 01224 Q_D(QFile); 01225 d->error = NoError; 01226 01227 qint64 ret = -1; 01228 qint64 read = fileEngine()->read(data, len); 01229 if (read != -1) 01230 ret = read; 01231 01232 if(ret < 0) { 01233 QFile::FileError err = fileEngine()->error(); 01234 if(err == QFile::UnspecifiedError) 01235 err = QFile::ReadError; 01236 d->setError(err, fileEngine()->errorString()); 01237 } 01238 return ret; 01239 }
Here is the call graph for this function:

Implements QIODevice.
Definition at line 1246 of file qfile.cpp.
References d, QAbstractFileEngine::error(), QIODevice::errorString(), fileEngine(), NoError, UnspecifiedError, QAbstractFileEngine::write(), and WriteError.
01247 { 01248 Q_D(QFile); 01249 d->error = NoError; 01250 01251 QAbstractFileEngine *fe = d->fileEngine ? d->fileEngine : fileEngine(); 01252 qint64 ret = fe->write(data, len); 01253 if(ret < 0) { 01254 QFile::FileError err = fileEngine()->error(); 01255 if(err == QFile::UnspecifiedError) 01256 err = QFile::WriteError; 01257 d->setError(err, fileEngine()->errorString()); 01258 } 01259 return ret; 01260 }
Here is the call graph for this function:

Reimplemented from QIODevice.
Definition at line 1213 of file qfile.cpp.
References fileEngine(), and QAbstractFileEngine::readLine().
01214 { 01215 return fileEngine()->readLine(data, maxlen); 01216 }
Here is the call graph for this function:

1.5.1