⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfile.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        return true;    }    return false;}/*! \fn QFile::open(OpenMode, int)    Use open(int, OpenMode) instead.*//*!    \overload    Opens the existing file descripter \a fd in the given \a 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.    \warning If \a fd is 0 (\c stdin), 1 (\c stdout), or 2 (\c    stderr), you may not be able to seek(). size() is set to \c    LLONG_MAX (in \c <climits>).    \sa close()*/bool QFile::open(int fd, 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, fd)) {        setOpenMode(mode);        if (mode & Append)            seek(size());        return true;    }    return false;}/*!  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.  \sa QSocketNotifier*/intQFile::handle() const{    if (!isOpen())        return -1;    if (QAbstractFileEngine *engine = fileEngine())        return engine->handle();    return -1;}/*!    \fn QString QFile::name() const    Use fileName() instead.*//*!    \fn void QFile::setName(const QString &name)    Use setFileName() instead.*//*!    Sets the file size (in bytes) \a sz. Returns true if the file if the    resize succeeds; false otherwise. If \a sz is larger than the file    currently is the new bytes will be set to 0, if \a sz is smaller the    file is simply truncated.    \sa size(), setFileName()*/boolQFile::resize(qint64 sz){    Q_D(QFile);    if (!d->ensureFlushed())        return false;    if (isOpen() && fileEngine()->pos() > sz)        seek(sz);    if(fileEngine()->setSize(sz)) {        unsetError();        return true;    }    d->setError(QFile::ResizeError, errno);    return false;}/*!    \overload    Sets \a fileName to size (in bytes) \a sz. Returns true if the file if    the resize succeeds; false otherwise. If \a sz is larger than \a    fileName currently is the new bytes will be set to 0, if \a sz is    smaller the file is simply truncated.    \sa resize()*/boolQFile::resize(const QString &fileName, qint64 sz){    return QFile(fileName).resize(sz);}/*!    Returns the complete OR-ed together combination of    QFile::Permission for the file.    \sa setPermissions(), setFileName()*/QFile::PermissionsQFile::permissions() const{    QAbstractFileEngine::FileFlags perms = fileEngine()->fileFlags(QAbstractFileEngine::PermsMask) & QAbstractFileEngine::PermsMask;    return QFile::Permissions((int)perms); //ewww}/*!    \overload    Returns the complete OR-ed together combination of    QFile::Permission for \a fileName.*/QFile::PermissionsQFile::permissions(const QString &fileName){    return QFile(fileName).permissions();}/*!    Sets the permissions for the file to the \a permissions specified.    Returns true if successful, or false if the permissions cannot be    modified.    \sa permissions(), setFileName()*/boolQFile::setPermissions(Permissions permissions){    Q_D(QFile);    if(fileEngine()->setPermissions(permissions)) {        unsetError();        return true;    }    d->setError(QFile::PermissionsError, errno);    return false;}/*!    \overload    Sets the permissions for \a fileName file to \a permissions.*/boolQFile::setPermissions(const QString &fileName, Permissions permissions){    return QFile(fileName).setPermissions(permissions);}static inline qint64 _qfile_writeData(QAbstractFileEngine *engine, QRingBuffer *buffer){    qint64 ret = engine->write(buffer->readPointer(), buffer->size());    if (ret > 0)        buffer->free(ret);    return ret;}/*!    Flushes any buffered data to the file. Returns true if successful;    otherwise returns false.*/boolQFile::flush(){    Q_D(QFile);    if (!d->writeBuffer.isEmpty()) {        if (!_qfile_writeData(d->fileEngine ? d->fileEngine : fileEngine(),                              &d->writeBuffer)) {            QFile::FileError err = fileEngine()->error();            if(err == QFile::UnspecifiedError)                err = QFile::WriteError;            d->setError(err, fileEngine()->errorString());            return false;        }    }    if (!fileEngine()->flush()) {        QFile::FileError err = fileEngine()->error();        if(err == QFile::UnspecifiedError)            err = QFile::WriteError;        d->setError(err, fileEngine()->errorString());        return false;    }    return true;}/*!  \reimp*/voidQFile::close(){    Q_D(QFile);    if(!isOpen())        return;    flush();    QIODevice::close();    unsetError();    if(!fileEngine()->close())        d->setError(fileEngine()->error(), fileEngine()->errorString());}/*!  Returns the size of the file.  For regular empty files on Unix (e.g. those in \c /proc), this function  returns 0; the contents of such a file are generated on demand in response  to you calling read().*/qint64 QFile::size() const{    Q_D(const QFile);    if (!d->ensureFlushed())        return 0;    return fileEngine()->size();}/*!  \reimp*/qint64 QFile::pos() const{    return QIODevice::pos();}/*!  Returns true if the end of the file has been reached; otherwise returns  false.  For regular empty files on Unix (e.g. those in \c /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.*/bool QFile::atEnd() const{    Q_D(const QFile);    if (!isOpen())        return true;    if (!d->ensureFlushed())        return false;    // If there's buffered data left, we're not at the end.    if (!d->buffer.isEmpty())        return false;    // If the file engine knows best, say what it says.    if (fileEngine()->supportsExtension(QAbstractFileEngine::AtEndExtension)) {        // Check if the file engine supports AtEndExtension, and if it does,        // check if the file engine claims to be at the end.        return fileEngine()->atEnd();    }    // Fall back to checking how much is available (will stat files).    return bytesAvailable() == 0;}/*!  \reimp*/bool QFile::seek(qint64 off){    Q_D(QFile);    if (!isOpen()) {        qWarning("QFile::seek: IODevice is not open");        return false;    }    if (!d->ensureFlushed())        return false;    if (!fileEngine()->seek(off) || !QIODevice::seek(off)) {        QFile::FileError err = fileEngine()->error();        if(err == QFile::UnspecifiedError)            err = QFile::PositionError;        d->setError(err, fileEngine()->errorString());        return false;    }    d->error = NoError;    return true;}/*!  \reimp*/qint64 QFile::readLineData(char *data, qint64 maxlen){    Q_D(QFile);    if (!d->ensureFlushed())        return -1;    if (fileEngine()->supportsExtension(QAbstractFileEngine::FastReadLineExtension))        return fileEngine()->readLine(data, maxlen);    // Fall back to QIODevice's readLine implementation if the engine    // cannot do it faster.    return QIODevice::readLineData(data, maxlen);}/*!  \reimp*/qint64 QFile::readData(char *data, qint64 len){    Q_D(QFile);    d->error = NoError;    if (!d->ensureFlushed())        return -1;    qint64 ret = -1;    qint64 read = fileEngine()->read(data, len);    if (read != -1)        ret = read;    if(ret < 0) {        QFile::FileError err = fileEngine()->error();        if(err == QFile::UnspecifiedError)            err = QFile::ReadError;        d->setError(err, fileEngine()->errorString());    }    return ret;}/*!    \internal*/bool QFilePrivate::putCharHelper(char c){#ifdef QT_NO_QOBJECT    return QIODevicePrivate::putCharHelper(c);#else    // Cutoff for code that doesn't only touch the buffer.    int writeBufferSize = writeBuffer.size();    if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE#ifdef Q_OS_WIN        || ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE)#endif        ) {        return QIODevicePrivate::putCharHelper(c);    }    if (!(openMode & QIODevice::WriteOnly)) {        if (openMode == QIODevice::NotOpen)            qWarning("QIODevice::putChar: Closed device");        else            qWarning("QIODevice::putChar: ReadOnly device");        return false;    }    // Make sure the device is positioned correctly.    const bool sequential = isSequential();    if (pos != devicePos && !sequential && !q_func()->seek(pos))        return false;    lastWasWrite = true;    int len = 1;#ifdef Q_OS_WIN    if ((openMode & QIODevice::Text) && c == '\n') {        ++len;        *writeBuffer.reserve(1) = '\r';    }#endif    // Write to buffer.    *writeBuffer.reserve(1) = c;    if (!sequential) {        pos += len;        devicePos += len;        if (!buffer.isEmpty())            buffer.skip(len);    }    return true;#endif}/*!  \reimp*/qint64QFile::writeData(const char *data, qint64 len){    Q_D(QFile);    d->error = NoError;    d->lastWasWrite = true;    bool buffered = !(d->openMode & Unbuffered);    // Flush buffered data if this read will overflow.    if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) {        if (!flush())            return -1;    }    // Write directly to the engine if the block size is larger than    // the write buffer size.    if (!buffered || len > QFILE_WRITEBUFFER_SIZE) {        QAbstractFileEngine *fe = d->fileEngine ? d->fileEngine : fileEngine();        qint64 ret = fe->write(data, len);        if(ret < 0) {            QFile::FileError err = fileEngine()->error();            if(err == QFile::UnspecifiedError)                err = QFile::WriteError;            d->setError(err, fileEngine()->errorString());        }        return ret;    }    // Write to the buffer.    char *writePointer = d->writeBuffer.reserve(len);    if (len == 1)        *writePointer = *data;    else        ::memcpy(writePointer, data, len);    return len;}/*!    \internal    Returns the QIOEngine for this QFile object.*/QAbstractFileEngine *QFile::fileEngine() const{    Q_D(const QFile);    if(!d->fileEngine)        d->fileEngine = QAbstractFileEngine::create(d->fileName);    return d->fileEngine;}/*!    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.    \sa unsetError()*/QFile::FileErrorQFile::error() const{    Q_D(const QFile);    return d->error;}/*!    Sets the file's error to QFile::NoError.    \sa error()*/voidQFile::unsetError(){    Q_D(QFile);    d->setError(QFile::NoError);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -