qfile.cpp
来自「Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3」· C++ 代码 · 共 727 行 · 第 1/2 页
CPP
727 行
return -1; }#endif Q_LONG nread; // number of bytes read if ( isRaw() ) { // raw file nread = QIODevice::readLine( p, maxlen ); } else { // buffered file p = fgets( p, maxlen, fh ); if ( p ) { nread = qstrlen( p ); if ( !isSequentialAccess() ) ioIndex += nread; } else { nread = -1; setStatus(IO_ReadError); setErrorString( qt_fileerr_read ); } } return nread;}/*! \overload Reads a line of text. Reads bytes from the file into string \a s, until end-of-line or \a 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 \c IO_Raw flag. Note that the string is read as plain Latin1 bytes, not Unicode. \sa readBlock(), QTextStream::readLine()*/Q_LONG QFile::readLine( QString& s, Q_ULONG maxlen ){ QByteArray ba(maxlen); Q_LONG l = readLine(ba.data(),maxlen); if ( l >= 0 ) { ba.truncate(l); s = QString(ba); } return l;}/*! Reads a single byte/character from the file. Returns the byte/character read, or -1 if the end of the file has been reached. \sa putch(), ungetch()*/int QFile::getch(){#if defined(QT_CHECK_STATE) if ( !isOpen() ) { // file not open qWarning( "QFile::getch: File not open" ); return EOF; } if ( !isReadable() ) { // reading not permitted qWarning( "QFile::getch: Read operation not permitted" ); return EOF; }#endif int ch; if ( !ungetchBuffer.isEmpty() ) { int len = ungetchBuffer.length(); ch = ungetchBuffer[ len-1 ]; ungetchBuffer.truncate( len - 1 ); return ch; } if ( isRaw() ) { // raw file (inefficient) char buf[1]; ch = readBlock( buf, 1 ) == 1 ? buf[0] : EOF; } else { // buffered file if ( (ch = getc( fh )) != EOF ) { if ( !isSequentialAccess() ) ioIndex++; } else { setStatus(IO_ReadError); setErrorString( qt_fileerr_read ); } } return ch;}/*! Writes the character \a ch to the file. Returns \a ch, or -1 if some error occurred. \sa getch(), ungetch()*/int QFile::putch( int ch ){#if defined(QT_CHECK_STATE) if ( !isOpen() ) { // file not open qWarning( "QFile::putch: File not open" ); return EOF; } if ( !isWritable() ) { // writing not permitted qWarning( "QFile::putch: Write operation not permitted" ); return EOF; }#endif if ( isRaw() ) { // raw file (inefficient) char buf[1]; buf[0] = ch; ch = writeBlock( buf, 1 ) == 1 ? ch : EOF; } else { // buffered file if ( (ch = putc( ch, fh )) != EOF ) { if ( !isSequentialAccess() ) ioIndex++; if ( ioIndex > length ) // update file length length = ioIndex; } else { setStatus(IO_WriteError); setErrorString( qt_fileerr_write ); } } return ch;}/*! Puts the character \a 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 \a ch, or -1 if an error occurred. \sa getch(), putch()*/int QFile::ungetch( int ch ){#if defined(QT_CHECK_STATE) if ( !isOpen() ) { // file not open qWarning( "QFile::ungetch: File not open" ); return EOF; } if ( !isReadable() ) { // reading not permitted qWarning( "QFile::ungetch: Read operation not permitted" ); return EOF; }#endif if ( ch == EOF ) // cannot unget EOF return ch; if ( isSequentialAccess() && !fh) { // pipe or similar => we cannot ungetch, so do it manually ungetchBuffer +=ch; return ch; } if ( isRaw() ) { // raw file (very inefficient) char buf[1]; at( ioIndex-1 ); buf[0] = ch; if ( writeBlock(buf, 1) == 1 ) at ( ioIndex-1 ); else ch = EOF; } else { // buffered file if ( (ch = ungetc(ch, fh)) != EOF ) { if ( !isSequentialAccess() ) ioIndex--; } else { setStatus( IO_ReadError ); setErrorString( qt_fileerr_read ); } } return ch;}static QCString locale_encoder( const QString &fileName ){ return fileName.local8Bit();}static QFile::EncoderFn encoder = locale_encoder;/*! 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 \a 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. \sa decodeName()*/QCString QFile::encodeName( const QString &fileName ){ return (*encoder)(fileName);}/*! \enum QFile::EncoderFn This is used by QFile::setEncodingFunction().*//*! \nonreentrant Sets the function for encoding Unicode file names to \a f. The default encodes in the locale-specific 8-bit encoding. \sa encodeName()*/void QFile::setEncodingFunction( EncoderFn f ){ encoder = f;}staticQString locale_decoder( const QCString &localFileName ){ return QString::fromLocal8Bit(localFileName);}static QFile::DecoderFn decoder = locale_decoder;/*! This does the reverse of QFile::encodeName() using \a localFileName. \sa setDecodingFunction()*/QString QFile::decodeName( const QCString &localFileName ){ return (*decoder)(localFileName);}/*! \enum QFile::DecoderFn This is used by QFile::setDecodingFunction().*//*! \nonreentrant Sets the function for decoding 8-bit file names to \a f. The default uses the locale-specific 8-bit encoding. \sa encodeName(), decodeName()*/void QFile::setDecodingFunction( DecoderFn f ){ decoder = f;}/*! Returns a human readable description of the reason of an error that occured on the device. The error described by the string corresponds to changes of QIODevice::status(). If the status is reset, the error string is also reset. The returned strings are not translated with the QObject::tr() or QApplication::translate() functions. They are marked as translatable strings in the context \c QFile. Before you show the string to the user you should translate it first, e.g: \code QFile f( "foo.txt" ); if ( !f.open( IO_ReadOnly ) { QMessageBox::critical( this, tr("Open failed"), tr("Could not open file for reading: %1").arg( qApp->translate("QFile",f.errorString()) ) ); return; } \endcode \sa QIODevice::status(), QIODevice::resetStatus(), setErrorString()*/QString QFile::errorString() const{ if ( status() == IO_Ok ) return qt_fileerr_unknown; return d->errorString;}/*! \nonreentrant Sets the error string returned by the errorString() function to \a str. \sa errorString(), QIODevice::status()*/void QFile::setErrorString( const QString& str ){ d->errorString = str;}void QFile::setErrorStringErrno( int errnum ){ switch ( errnum ) { case EACCES: d->errorString = QFILEERR_EACCES; break; case EMFILE: d->errorString = QFILEERR_EMFILE; break; case ENOENT: d->errorString = QFILEERR_ENOENT; break; case ENOSPC: d->errorString = QFILEERR_ENOSPC; break; default:#ifndef Q_OS_TEMP d->errorString = QString::fromLocal8Bit( strerror( errnum ) );#else { unsigned short *string; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&string, 0, NULL ); d->errorString = QString::fromUcs2( string ); LocalFree( (HLOCAL)string ); }#endif break; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?