📄 qfile.3qt
字号:
.TH QFile 3qt "6 July 1999" "Troll Tech AS" \" -*- nroff -*-.\" Copyright 1992-1999 Troll Tech AS. All rights reserved. See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQFile \- I/O device that operates on files.SH SYNOPSIS.br.PP\fC#include <qfile.h>\fR.PPInherits QIODevice..PP.SS "Public Members".in +1c.ti -1c.BI "\fBQFile\fR ()".br.ti -1c.BI "\fBQFile\fR ( const QString & name )".br.ti -1c.BI "\fB~QFile\fR ()".br.ti -1c.BI "QString \fBname\fR () const".br.ti -1c.BI "void \fBsetName\fR ( const QString & name )".br.ti -1c.BI "typedef \fBQCString\fR ( * EncoderFn )( const QString & " "fileName" ")".br.ti -1c.BI "typedef \fBQString\fR ( * DecoderFn )( const QCString & " "localfileName" ")".br.ti -1c.BI "bool \fBexists\fR () const".br.ti -1c.BI "bool \fBremove\fR ()".br.ti -1c.BI "virtual bool \fBopen\fR ( int )".br.ti -1c.BI "bool \fBopen\fR ( int, FILE * )".br.ti -1c.BI "bool \fBopen\fR ( int, int )".br.ti -1c.BI "virtual void \fBclose\fR ()".br.ti -1c.BI "virtual void \fBflush\fR ()".br.ti -1c.BI "virtual uint \fBsize\fR () const".br.ti -1c.BI "virtual int \fBat\fR () const".br.ti -1c.BI "virtual bool \fBat\fR ( int )".br.ti -1c.BI "virtual bool \fBatEnd\fR () const".br.ti -1c.BI "virtual int \fBreadBlock\fR ( char * " "data" ", uint len )".br.ti -1c.BI "virtual int \fBwriteBlock\fR ( const char * " "data" ", uint len )".br.ti -1c.BI "int \fBwriteBlock\fR ( const QByteArray & data )".br.ti -1c.BI "virtual int \fBreadLine\fR ( char * " "data" ", uint maxlen )".br.ti -1c.BI "int \fBreadLine\fR ( QString &, uint maxlen )".br.ti -1c.BI "virtual int \fBgetch\fR ()".br.ti -1c.BI "virtual int \fBputch\fR ( int )".br.ti -1c.BI "virtual int \fBungetch\fR ( int )".br.ti -1c.BI "int \fBhandle\fR () const".br.in -1c.SS "Static Public Members".in +1c.ti -1c.BI "QCString \fBencodeName\fR ( const QString & fileName )".br.ti -1c.BI "QString \fBdecodeName\fR ( const QCString & localFileName )".br.ti -1c.BI "void \fBsetEncodingFunction\fR ( EncoderFn )".br.ti -1c.BI "void \fBsetDecodingFunction\fR ( DecoderFn )".br.ti -1c.BI "bool \fBexists\fR ( const QString & fileName )".br.ti -1c.BI "bool \fBremove\fR ( const QString & fileName )".br.in -1c.SH DESCRIPTIONThe QFile class is an I/O device that operates on files..PPQFile is an I/O device for reading and writing binary and text files. A QFile may be used by itself (readBlock and writeBlock) or by more conveniently using QDataStream or QTextStream..PPHere is a code fragment that uses QTextStream to read a text file line by line. It prints each line with a line number..PP.nf.br QFile f("file.txt");.br if ( f.open(IO_ReadOnly) ) { // file opened successfully.br QTextStream t( &f ); // use a text stream.br QString s;.br int n = 1;.br while ( !t.eof() ) { // until end of file....br s = t.readLine(); // line of text excluding '\\n'.br printf( "%3d: %s\\n", n++, (const char *)s );.br }.br f.close();.br }.fi.PPThe QFileInfo class holds detailed information about a file, such as access permissions, file dates and file types..PPThe QDir class manages directories and lists of file names..PPSee also: QDataStream and QTextStream..SH MEMBER FUNCTION DOCUMENTATION.SH "QFile::QFile ()"Constructs a QFile with no name..SH "QFile::QFile ( const QString & name )"Constructs a QFile with a file name \fIname.\fR.PPSee also: setName()..SH "QFile::~QFile ()"Destroys a QFile. Calls close()..SH "bool QFile::at ( int pos ) \fC[virtual]\fR"Sets the file index to \fIpos.\fR Returns TRUE if successful, otherwise FALSE..PPExample:.PP.nf.br QFile f( "data.bin" );.br f.open( IO_ReadOnly ); // index set to 0.br f.at( 100 ); // set index to 100.br f.at( f.at()+50 ); // set index to 150.br f.at( f.size()-80 ); // set index to 80 before EOF.br f.close();.fi.PP\fBWarning:\fR The result is undefined if the file was opened using the \fCIO_Append\fR specifier..PPSee also: size() and open()..PPReimplemented from QIODevice..SH "int QFile::at () const \fC[virtual]\fR"Returns the file index..PPSee also: size()..PPReimplemented from QIODevice..SH "bool QFile::atEnd () const \fC[virtual]\fR"Returns TRUE if the end of file has been reached, otherwise FALSE..PPSee also: size()..PPReimplemented from QIODevice..SH "void QFile::close () \fC[virtual]\fR"Closes an open file..PPThe file is closed even if it was opened with an existing file handle or a file descriptor, \fIexcept\fR that stdin, stdout and stderr are never closed..PPSome "write-behind" filesystems may report an unspecified error on closing the file. These errors only indiciate that something may have gone wrong since the previous open(). In such a case status() reports IO_UnspecifiedError after close(), otherwise IO_Ok..PPSee also: open() and flush()..PPReimplemented from QIODevice..SH "QString QFile::decodeName ( const QCString & localFileName ) \fC[static]\fR"This does the reverse of QFile::encodeName()..PPSee also: setDecodingFunction()..SH "QCString QFile::encodeName ( const QString & fileName ) \fC[static]\fR"When you use QFile, QFileInfo, and QDir to access the filesystem with Qt, you can use Unicode filenames. On Unix, these filenames are converted to an 8-bit encoding. If you want to do your own file I/O on Unix, you should convert the filename using this function. On Windows NT, Unicode filenames are supported directly in the filesystem and this function should be avoided. On Windows 95, non-Latin1 locales are not supported at this time..PPBy default, this function converts to the local 8-bit encoding determined by the user's locale. This is sufficient for filenames that the user chooses. Filenames hard-coded into the application should only use 7-bit ASCII filename characters..PPThe conversion scheme can be changed using setEncodingFunction(). This might be useful if you wish to give the user an option to store in filenames in UTF-8, etc., but beware that such filenames would probably then be unrecognizable when seen by other programs..PPSee also: decodedName()..SH "bool QFile::exists () const"Returns TRUE if this file exists, otherwise FALSE..PPSee also: name()..SH "bool QFile::exists ( const QString & fileName ) \fC[static]\fR"Returns TRUE if the file given by \fIfileName\fR exists, otherwise FALSE..SH "void QFile::flush () \fC[virtual]\fR"Flushes the file buffer to the disk..PPclose() also flushes the file buffer..PPReimplemented from QIODevice..SH "int QFile::getch () \fC[virtual]\fR"Reads a single byte/character from the file..PPReturns the byte/character read, or -1 if the end of the file has been reached..PPSee also: putch() and ungetch()..PPReimplemented from QIODevice..SH "int QFile::handle () const"Returns the file handle of the file..PPThis is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(), as well as with QSocketNotifier..PPIf the file is not open or there is an error, handle() returns -1..PPSee also: QSocketNotifier..SH "QString QFile::name () const"Returns the name set by setName()..PPSee also: setName() and QFileInfo::fileName()..SH "bool QFile::open ( int m ) \fC[virtual]\fR"Opens the file specified by the file name currently set, using the mode \fIm.\fR Returns TRUE if successful, otherwise FALSE..PPThe mode parameter \fIm\fR must be a combination of the following flags:.TP\fCIO_Raw\fR specified raw (non-buffered) file access..TP\fCIO_ReadOnly\fR opens the file in read-only mode..TP\fCIO_WriteOnly\fR opens the file in write-only mode (and truncates)..TP\fCIO_ReadWrite\fR opens the file in read/write mode, equivalent to \fC(IO_ReadOnly|IO_WriteOnly).\fR.TP\fCIO_Append\fR opens the file in append mode. This mode is very useful when you want to write something to a log file. The file index is set to the end of the file. Note that the result is undefined if you position the file index manually using at() in append mode..TP\fCIO_Truncate\fR truncates the file..TP\fCIO_Translate\fR enables carriage returns and linefeed translation for text files under MS-DOS, Windows and OS/2..PPThe raw access mode is best when I/O is block-operated using 4kB block size or greater. Buffered access works better when reading small portions of data at a time..PP\fBImportant:\fR When working with buffered files, data may not be written to the file at once. Call flush to make sure the data is really written..PP\fBWarning:\fR We have experienced problems with some C libraries when a buffered file is opened for both reading and writing. If a read operation takes place immediately after a write operation, the read buffer contains garbage data. Worse, the same garbage is written to the file. Calling flush() before readBlock() solved this problem..PPIf the file does not exist and \fCIO_WriteOnly\fR or \fCIO_ReadWrite\fR is specified, it is created..PPExample:.PP.nf.br QFile f1( "/tmp/data.bin" );.br QFile f2( "readme.txt" );.br f1.open( IO_Raw | IO_ReadWrite | IO_Append );.br f2.open( IO_ReadOnly | IO_Translate );.fi.PPSee also: name(), close(), isOpen() and flush()..PPReimplemented from QIODevice..SH "bool QFile::open ( int m, FILE * f )"Opens a file in the mode \fIm\fR using an existing file handle \fIf.\fR Returns TRUE if successful, otherwise FALSE..PPExample:.PP.nf.br #include <stdio.h>.br.br void printError( const char* msg ).br {.br QFile f;.br f.open( IO_WriteOnly, stderr );.br f.writeBlock( msg, strlen(msg) ); // write to stderr.br f.close();.br }.fi.PPWhen a QFile is opened using this function, close() does not actually close the file, only flushes it..PP\fBWarning:\fR If \fIf\fR is \fCstdin, stdout, stderr,\fR you may not be able to seek. See QIODevice::isSequentialAccess() for more information..PPSee also: close()..SH "bool QFile::open ( int m, int f )"Opens a file in the mode \fIm\fR using an existing file descriptor \fIf.\fR Returns TRUE if successful, otherwise FALSE..PPWhen a QFile is opened using this function, close() does not actually close the file..PP\fBWarning:\fR If \fIf\fR is one of 0 (stdin), 1 (stdout) or 2 (stderr), you may not be able to seek. size() is set to \fCINT_MAX\fR (in limits.h)..PPSee also: close()..SH "int QFile::putch ( int ch ) \fC[virtual]\fR"Writes the character \fIch\fR to the file..PPReturns \fIch,\fR or -1 if some error occurred..PPSee also: getch() and ungetch()..PPReimplemented from QIODevice..SH "int QFile::readBlock ( char * p, uint len ) \fC[virtual]\fR"Reads at most \fIlen\fR bytes from the file into \fIp\fR and returns the number of bytes actually read..PPReturns -1 if a serious error occurred..PP\fBWarning:\fR We have experienced problems with some C libraries when a buffered file is opened for both reading and writing. If a read operation takes place immediately after a write operation, the read buffer contains garbage data. Worse, the same garbage is written to the file. Calling flush() before readBlock() solved this problem..PPSee also: writeBlock()..PPReimplemented from QIODevice..SH "int QFile::readLine ( QString & s, uint maxlen )"Reads a line of text..PPReads bytes from the file until end-of-line is reached, or up to \fImaxlen\fR bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped..PPThis function is efficient only for buffered files. Avoid readLine() for files that have been opened with the \fCIO_Raw\fR flag..PPNote that the string is read as plain Latin1 bytes, not Unicode..PPSee also: readBlock() and QTextStream::readLine()..SH "int QFile::readLine ( char * p, uint maxlen ) \fC[virtual]\fR"Reads a line of text..PPReads bytes from the file until end-of-line is reached, or up to \fImaxlen\fR bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped..PPThis function is efficient only for buffered files. Avoid readLine() for files that have been opened with the \fCIO_Raw\fR flag..PPSee also: readBlock() and QTextStream::readLine()..PPReimplemented from QIODevice..SH "bool QFile::remove ()"Removes the file specified by the file name currently set. Returns TRUE if successful, otherwise FALSE..PPThe file is closed before it is removed..SH "bool QFile::remove ( const QString & fileName ) \fC[static]\fR"Removes the file \fIfileName.\fR Returns TRUE if successful, otherwise FALSE..SH "void QFile::setDecodingFunction ( DecoderFn f ) \fC[static]\fR"Sets the function for decoding 8-bit filenames. The default uses the locale-specific 8-bit encoding..PPSee also: encodeName() and decodedName()..SH "void QFile::setEncodingFunction ( EncoderFn f ) \fC[static]\fR"Sets the function for encoding Unicode filenames. The default encodes in the locale-specific 8-bit encoding..PPSee also: encodeName()..SH "void QFile::setName ( const QString & name )"Sets the name of the file. The name can include an absolute directory path or it can be a name or a path relative to the current directory..PPDo not call this function if the file has already been opened..PPNote that if the name is relative QFile does not associate it with the current directory. If you change directory before calling open(), open uses the new current directory..PPExample:.PP.nf.br QFile f;.br QDir::setCurrent( "/tmp" );.br f.setName( "readme.txt" );.br QDir::setCurrent( "/home" );.br f.open( IO_ReadOnly ); // opens "/home/readme.txt" under UNIX.fi.PPAlso note that the directory separator '/' works for all operating systems supported by Qt..PPSee also: name(), QFileInfo and QDir..SH "uint QFile::size () const \fC[virtual]\fR"Returns the file size..PPSee also: at()..PPReimplemented from QIODevice..SH "int QFile::ungetch ( int ch ) \fC[virtual]\fR"Puts the character \fIch\fR back into the file and decrements the index if it is not zero..PPThis function is normally called to "undo" a getch() operation..PPReturns \fIch,\fR or -1 if some error occurred..PPSee also: getch() and putch()..PPReimplemented from QIODevice..SH "int QFile::writeBlock ( const char * p, uint len ) \fC[virtual]\fR"Writes \fIlen\fR bytes from \fIp\fR to the file and returns the number of bytes actually written..PPReturns -1 if a serious error occurred..PP\fBImportant:\fR When working with buffered files, data may not be written to the file at once. Call flush to make sure the data is really written..PPSee also: readBlock()..PPReimplemented from QIODevice..SH "SEE ALSO".BR http://www.troll.no/qt/qfile.html.SH COPYRIGHTCopyright 1992-1999 Troll Tech AS. See the license file included inthe distribution for a complete license statement..SH AUTHORGenerated automatically from the source code.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -