The QFile class is an I/O device that operates on files. More...
#include <qfile.h>
Public Types | |
typedef QCString(* | EncoderFn) (const QString &fileName) |
typedef QString(* | DecoderFn) (const QCString &localfileName) |
Public Member Functions | |
QFile () | |
QFile (const QString &name) | |
virtual | ~QFile () |
QString | name () const |
void | setName (const QString &name) |
bool | exists () const |
bool | remove () |
bool | open (int) |
bool | open (int, FILE *) |
bool | open (int, int) |
void | close () |
void | flush () |
uint | size () const |
int | at () const |
bool | at (int) |
bool | atEnd () const |
int | readBlock (char *data, uint len) |
int | writeBlock (const char *data, uint len) |
int | writeBlock (const QByteArray &data) |
int | readLine (char *data, uint maxlen) |
int | readLine (QString &, uint maxlen) |
int | getch () |
int | putch (int) |
int | ungetch (int) |
int | handle () const |
int64 | pos () const |
int64 | toEnd () |
bool | seek (int64 pos) |
Public Member Functions inherited from QIODevice | |
QIODevice () | |
virtual | ~QIODevice () |
int | flags () const |
int | mode () const |
int | state () const |
bool | isDirectAccess () const |
bool | isSequentialAccess () const |
bool | isCombinedAccess () const |
bool | isBuffered () const |
bool | isRaw () const |
bool | isSynchronous () const |
bool | isAsynchronous () const |
bool | isTranslated () const |
bool | isReadable () const |
bool | isWritable () const |
bool | isReadWrite () const |
bool | isInactive () const |
bool | isOpen () const |
int | status () const |
void | resetStatus () |
bool | reset () |
int | writeBlock (const QByteArray &data) |
QByteArray | readAll () |
Static Public Member Functions | |
static QCString | encodeName (const QString &fileName) |
static QString | decodeName (const QCString &localFileName) |
static void | setEncodingFunction (EncoderFn) |
static void | setDecodingFunction (DecoderFn) |
static bool | exists (const QString &fileName) |
static bool | remove (const QString &fileName) |
Protected Attributes | |
QString | fn |
FILE * | fh |
int | fd |
int | length |
bool | ext_f |
void * | d |
Protected Attributes inherited from QIODevice | |
int | ioIndex |
Private Member Functions | |
void | init () |
QFile (const QFile &) | |
QFile & | operator= (const QFile &) |
Private Attributes | |
QCString | ungetchBuffer |
Additional Inherited Members | |
Protected Member Functions inherited from QIODevice | |
void | setFlags (int f) |
void | setType (int) |
void | setMode (int) |
void | setState (int) |
void | setStatus (int) |
The QFile class is an I/O device that operates on files.
QFile 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.
Here is a code fragment that uses QTextStream to read a text file line by line. It prints each line with a line number.
The QFileInfo class holds detailed information about a file, such as access permissions, file dates and file types.
The QDir class manages directories and lists of file names.
QFile::QFile | ( | ) |
Constructs a QFile with no name.
QFile::QFile | ( | const QString & | name | ) |
|
private |
|
inlinevirtual |
|
virtual |
|
virtual |
|
virtual |
Closes an open file.
The file is not closed if it was opened with an existing file handle. If the existing file handle is a FILE*
, the file is flushed. If the existing file handle is an int
file descriptor, nothing is done to the file.
Some "write-behind" filesystems may report an unspecified error on closing the file. These errors only indicate that something may have gone wrong since the previous open(). In such a case status() reports IO_UnspecifiedError after close(), otherwise IO_Ok.
Implements QIODevice.
Definition at line 614 of file qfile_unix.cpp.
This does the reverse of QFile::encodeName().
Definition at line 529 of file qfile.cpp.
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.
By 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.
The 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.
Definition at line 494 of file qfile.cpp.
bool QFile::exists | ( | ) | const |
|
virtual |
|
virtual |
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(), as well as with QSocketNotifier.
If the file is not open or there is an error, handle() returns -1.
Definition at line 587 of file qfile_unix.cpp.
|
inline |
Returns the name set by setName().
Definition at line 121 of file qfile.h.
|
virtual |
Opens the file specified by the file name currently set, using the mode m. Returns TRUE if successful, otherwise FALSE.
The mode parameter m must be a combination of the following flags:
IO_Raw
specified raw (non-buffered) file access. IO_ReadOnly
opens the file in read-only mode. IO_WriteOnly
opens the file in write-only mode (and truncates). IO_ReadWrite
opens the file in read/write mode, equivalent to
(IO_ReadOnly|IO_WriteOnly). IO_Append
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. IO_Truncate
truncates the file. IO_Translate
enables carriage returns and linefeed translation for text files under MS-DOS, Windows and OS/2. The 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.
Important: 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.
If the file does not exist and IO_WriteOnly
or IO_ReadWrite
is specified, it is created.
Implements QIODevice.
Definition at line 134 of file qfile_unix.cpp.
bool QFile::open | ( | int | m, |
FILE * | f | ||
) |
Opens a file in the mode m using an existing file handle f. Returns TRUE if successful, otherwise FALSE.
When a QFile is opened using this function, close() does not actually close the file, only flushes it.
stdin
, stdout
, stderr
, you may not be able to seek. See QIODevice::isSequentialAccess() for more information.Definition at line 306 of file qfile_unix.cpp.
bool QFile::open | ( | int | m, |
int | f | ||
) |
Opens a file in the mode m using an existing file descriptor f. Returns TRUE if successful, otherwise FALSE.
When a QFile is opened using this function, close() does not actually close the file.
INT_MAX
(in limits.h).Definition at line 356 of file qfile_unix.cpp.
int64 QFile::pos | ( | ) | const |
Definition at line 637 of file qfile_unix.cpp.
|
virtual |
|
virtual |
Reads at most len bytes from the file into p and returns the number of bytes actually read.
Returns -1 if a serious error occurred.
Implements QIODevice.
Definition at line 473 of file qfile_unix.cpp.
|
virtual |
Reads a line of text.
Reads bytes from the file until end-of-line is reached, or up to maxlen bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped.
This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw
flag.
Reimplemented from QIODevice.
Definition at line 268 of file qfile.cpp.
Reads a line of text.
Reads bytes from the file until end-of-line is reached, or up to maxlen bytes, and returns the number of bytes read, or -1 in case of error. The terminating newline is not stripped.
This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw
flag.
Note that the string is read as plain Latin1 bytes, not Unicode.
Definition at line 316 of file qfile.cpp.
bool QFile::remove | ( | ) |
Removes the file fileName. Returns TRUE if successful, otherwise FALSE.
Definition at line 66 of file qfile_unix.cpp.
Definition at line 660 of file qfile_unix.cpp.
|
static |
Sets the function for decoding 8-bit filenames. The default uses the locale-specific 8-bit encoding.
Definition at line 547 of file qfile.cpp.
|
static |
Sets the function for encoding Unicode filenames. The default encodes in the locale-specific 8-bit encoding.
Definition at line 511 of file qfile.cpp.
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.
Do not call this function if the file has already been opened.
Note 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.
Also note that the directory separator '/' works for all operating systems supported by Qt.
Definition at line 167 of file qfile.cpp.
|
virtual |
Returns the file size.
Implements QIODevice.
Definition at line 398 of file qfile_unix.cpp.
int64 QFile::toEnd | ( | ) |
Definition at line 647 of file qfile_unix.cpp.
|
virtual |
|
virtual |
Writes len bytes from p to the file and returns the number of bytes actually written.
Returns -1 if a serious error occurred.
Implements QIODevice.
Definition at line 537 of file qfile_unix.cpp.
|
inline |