📄 qfile.cpp
字号:
\fn void QFile::setDecodingFunction(DecoderFn function) \nonreentrant Sets the \a function for decoding 8-bit file names. The default uses the locale-specific 8-bit encoding. \sa setEncodingFunction(), decodeName()*/voidQFile::setDecodingFunction(DecoderFn f){ if (!f) f = locale_decode; QFilePrivate::decoder = f;}/*! \overload Returns true if the file specified by fileName() exists; otherwise returns false. \sa fileName(), setFileName()*/boolQFile::exists() const{ // 0x1000000 = QAbstractFileEngine::Refresh, forcing an update return (fileEngine()->fileFlags(QAbstractFileEngine::FlagsMask | QAbstractFileEngine::FileFlag(0x1000000)) & QAbstractFileEngine::ExistsFlag);}/*! Returns true if the file specified by \a fileName exists; otherwise returns false.*/boolQFile::exists(const QString &fileName){ return QFileInfo(fileName).exists();}/*! \fn QString QFile::symLinkTarget() const \since 4.2 \overload Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link. 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. \sa fileName() setFileName()*//*! \obsolete Use symLinkTarget() instead.*/QStringQFile::readLink() const{ return fileEngine()->fileName(QAbstractFileEngine::LinkName);}/*! \fn static QString QFile::symLinkTarget(const QString &fileName) \since 4.2 Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified by \a fileName, or returns an empty string if the \a fileName does not correspond to a symbolic link. 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.*//*! \obsolete Use symLinkTarget() instead.*/QStringQFile::readLink(const QString &fileName){ return QFileInfo(fileName).readLink();}/*! Removes the file specified by fileName(). Returns true if successful; otherwise returns false. The file is closed before it is removed. \sa setFileName()*/boolQFile::remove(){ Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::remove: Empty or null file name"); return false; } close(); if(error() == QFile::NoError) { if(fileEngine()->remove()) { unsetError(); return true; } d->setError(QFile::RemoveError, errno); } return false;}/*! \overload Removes the file specified by the \a fileName given. Returns true if successful; otherwise returns false. \sa remove()*/boolQFile::remove(const QString &fileName){ return QFile(fileName).remove();}/*! Renames the file currently specified by fileName() to \a newName. Returns true if successful; otherwise returns false. If a file with the name \a newName already exists, rename() returns false (i.e., QFile will not overwrite it). The file is closed before it is renamed. \sa setFileName()*/boolQFile::rename(const QString &newName){ Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::rename: Empty or null file name"); return false; } if (QFile(newName).exists()) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); d->setError(QFile::RenameError, QLatin1String("Destination file exists")); return false; } close(); if(error() == QFile::NoError) { if (fileEngine()->rename(newName)) { unsetError(); return true; } QFile in(fileName()); QFile out(newName); if (in.open(QIODevice::ReadOnly)) { if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { bool error = false; char block[4096]; while (!in.atEnd()) { qint64 read = in.read(block, sizeof(block)); if (read == -1) { d->setError(QFile::RenameError, in.errorString()); error = true; break; } if (read != out.write(block, read)) { d->setError(QFile::RenameError, out.errorString()); error = true; break; } } if(!error) in.remove(); return !error; } } d->setError(QFile::RenameError, out.isOpen() ? in.errorString() : out.errorString()); } return false;}/*! \overload Renames the file \a oldName to \a newName. Returns true if successful; otherwise returns false. If a file with the name \a newName already exists, rename() returns false (i.e., QFile will not overwrite it). \sa rename()*/boolQFile::rename(const QString &oldName, const QString &newName){ return QFile(oldName).rename(newName);}/*! Creates a link named \a 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. This function will not overwrite an already existing entity in the file system; in this case, \c link() will return false and set \l{QFile::}{error()} to return \l{QFile::}{RenameError}. \note To create a valid link on Windows, \a linkName must have a \c{.lnk} file extension. \sa setFileName()*/boolQFile::link(const QString &linkName){ Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::link: Empty or null file name"); return false; } QFileInfo fi(linkName); if(fileEngine()->link(fi.absoluteFilePath())) { unsetError(); return true; } d->setError(QFile::RenameError, errno); return false;}/*! \overload Creates a link named \a linkName that points to the file \a 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. \sa link()*/boolQFile::link(const QString &fileName, const QString &linkName){ return QFile(fileName).link(linkName);}/*! Copies the file currently specified by fileName() to a file called \a newName. Returns true if successful; otherwise returns false. Note that if a file with the name \a newName already exists, copy() returns false (i.e. QFile will not overwrite it). The source file is closed before it is copied. \sa setFileName()*/boolQFile::copy(const QString &newName){ Q_D(QFile); if (d->fileName.isEmpty()) { qWarning("QFile::copy: Empty or null file name"); return false; } if (QFile(newName).exists()) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); See also rename(). d->setError(QFile::CopyError, QLatin1String("Destination file exists")); return false; } close(); if(error() == QFile::NoError) { if(fileEngine()->copy(newName)) { unsetError(); return true; } else { bool error = false; if(!open(QFile::ReadOnly)) { error = true; QString errorMessage = QLatin1String("Cannot open %1 for input"); d->setError(QFile::CopyError, errorMessage.arg(d->fileName)); } else { QString fileTemplate = QLatin1String("%1/qt_temp.XXXXXX");#ifdef QT_NO_TEMPORARYFILE QFile out(fileTemplate.arg(QFileInfo(newName).path())); if (!out.open(QIODevice::ReadWrite)) error = true;#else QTemporaryFile out(fileTemplate.arg(QFileInfo(newName).path())); if (!out.open()) { out.setFileTemplate(fileTemplate.arg(QDir::tempPath())); if (!out.open()) error = true; }#endif if (error) { out.close(); d->setError(QFile::CopyError, QLatin1String("Cannot open for output")); } else { char block[4096]; qint64 totalRead = 0; while(!atEnd()) { qint64 in = read(block, sizeof(block)); if (in <= 0) break; totalRead += in; if(in != out.write(block, in)) { d->setError(QFile::CopyError, QLatin1String("Failure to write block")); error = true; break; } } if (totalRead != size()) { // Unable to read from the source. The error string is // already set from read(). error = true; } if (!error && !out.rename(newName)) { error = true; QString errorMessage = QLatin1String("Cannot create %1 for output"); d->setError(QFile::CopyError, errorMessage.arg(newName)); }#ifndef QT_NO_TEMPORARYFILE if (!error) out.setAutoRemove(false);#endif } } if(!error) { QFile::setPermissions(newName, permissions()); unsetError(); return true; } } } return false;}/*! \overload Copies the file \a fileName to \a newName. Returns true if successful; otherwise returns false. If a file with the name \a newName already exists, copy() returns false (i.e., QFile will not overwrite it). \sa rename()*/boolQFile::copy(const QString &fileName, const QString &newName){ return QFile(fileName).copy(newName);}/*! Returns true if the file can only be manipulated sequentially; otherwise returns false. Most files support random-access, but some special files may not. \sa QIODevice::isSequential()*/bool QFile::isSequential() const{ Q_D(const QFile); return d->fileEngine && d->fileEngine->isSequential();}/*! Opens the file using OpenMode \a mode, returning true if successful; otherwise false. The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered. \note In \l{QIODevice::}{WriteOnly} or \l{QIODevice::}{ReadWrite} mode, if the relevant file does not already exist, this function will try to create a new file before opening it. \note Because of limitations in the native API, QFile ignores the Unbuffered flag on Windows. \sa QIODevice::OpenMode, setFileName()*/bool QFile::open(OpenMode mode){ Q_D(QFile); if (isOpen()) { qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); return false; } if (mode & Append) mode |= WriteOnly; unsetError(); if ((mode & (ReadOnly | WriteOnly)) == 0) { qWarning("QIODevice::open: File access not specified"); return false; } if (fileEngine()->open(mode)) { setOpenMode(mode); if (mode & Append) seek(size()); return true; } QFile::FileError err = fileEngine()->error(); if(err == QFile::UnspecifiedError) err = QFile::OpenError; d->setError(err, fileEngine()->errorString()); return false;}/*! \fn QFile::open(OpenMode, FILE*) Use open(FILE *, OpenMode) instead.*//*! \overload Opens the existing file handle \a fh in the given \a mode. Returns true if successful; otherwise returns false. Example: \code #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(); } \endcode When a QFile is opened using this function, close() does not actually close the file, but only flushes it. \warning If \a fh is \c stdin, \c stdout, or \c stderr, you may not be able to seek(). See QIODevice::isSequentialAccess() for more information. \bold{Note:} On Windows, you need to enable support for console applications in order to use the stdin, stdout and stderr streams at the console. To do this, add the following declaration to your application's project file: \code CONFIG += console \endcode \note On Windows, \a fh must be opened in binary mode (i.e., the mode string must contain 'b', as in "rb" or "wb") when accessing files and other random-access devices. Qt will translate the end-of-line characters if you pass QIODevice::Text to \a mode. Sequential devices, such as stdin and stdout, are unaffected by this limitation. \sa close(), {qmake Variable Reference#CONFIG}{qmake Variable Reference}*/bool QFile::open(FILE *fh, OpenMode mode){ Q_D(QFile); if (isOpen()) { qWarning("QFile::open: File (%s) already open", qPrintable(fileName())); return false; } if (mode & Append) mode |= WriteOnly; unsetError(); if ((mode & (ReadOnly | WriteOnly)) == 0) { qWarning("QFile::open: File access not specified"); return false; } if(d->openExternalFile(mode, fh)) { setOpenMode(mode); if (mode & Append) { seek(size()); } else { long pos = ftell(fh); if (pos != -1) seek(pos); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -