Almost all the functions in this class are reentrant when TQt is built with thread support. The exceptions are \fBsetEncodingFunction\fR(), \fBsetDecodingFunction\fR(), and \fBsetErrorString\fR(). </p>
TQFile is an I/O device for reading and writing binary and text files. A TQFile may be used by itself or more conveniently with a TQDataStream or TQTextStream.
The file name is usually passed in the constructor but can be changed with setName(). You can check for a file's existence with exists() and remove a file with remove().
The file is opened with open(), closed with close() and flushed with flush(). Data is usually read and written using TQDataStream or TQTextStream, but you can read with readBlock() and readLine() and write with writeBlock(). TQFile also supports getch(), ungetch() and putch().
The size of the file is returned by size(). You can get the current file position or move to a new file position using the at() functions. If you've reached the end of the file, atEnd() returns TRUE. The file handle is returned by handle().
Qt uses Unicode file names. If you want to do your own I/O on Unix systems you may want to use encodeName() (and decodeName()) to convert the file name into the local encoding.
The file is not closed if it was opened with an existing file handle. If the existing file handle is a \fCFILE*\fR, the file is flushed. If the existing file handle is an \fCint\fR file descriptor, nothing is done to the file.
.PP
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.
When you use TQFile, TQFileInfo, and TQDir 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 \fIfileName\fR 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.
.PP
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.
Returns a human-readable description of the reason of an error that occurred on the device. The error described by the string corresponds to changes of TQIODevice::status(). If the status is reset, the error string is also reset.
The returned strings are not translated with the TQObject::tr() or QApplication::translate() functions. They are marked as translatable strings in the "TQFile" context. Before you show the string to the user you should translate it first, for example:
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 (ie. Unix systems, but not Windows) the handle can be used with TQSocketNotifier as well.
Opens the file specified by the file name currently set, using the mode \fIm\fR. Returns TRUE if successful, otherwise FALSE.
.PP
.PP
The mode parameter \fIm\fR must be a combination of the following flags: <center>.nf
.TS
l - l. Flag Meaning IO_Raw Raw (non-buffered) file access. IO_ReadOnly Opens the file in read-only mode. IO_WriteOnly Opens the file in write-only mode. If this flag is used with another flag, e.g. IO_ReadOnly or IO_Raw or IO_Append, the file is \fInot\fR truncated; but if used on its own (or with IO_Truncate), the file is truncated. IO_ReadWrite Opens the file in read/write mode, equivalent to IO_Append Opens the file in append mode. (You must actually use IO_Truncate Truncates the file. IO_Translate
.TE
.fi
</center>
.PP
The raw access mode is best when I/O is block-operated using a 4KB block size or greater. Buffered access works better when reading small portions of data at a time.
.PP
\fBWarning:\fR When working with buffered files, data may not be written to the file at once. Call flush() to make sure that the data is really written.
.PP
\fBWarning:\fR If you have a buffered file opened for both reading and writing you must not perform an input operation immediately after an output operation or vice versa. You should always call flush() or a file positioning operation, e.g. at(), between input and output operations, otherwise the buffer may contain garbage.
.PP
If the file does not exist and IO_WriteOnly or IO_ReadWrite is specified, it is created.
\fBWarning:\fR If \fIf\fR is \fCstdin\fR, \fCstdout\fR, \fCstderr\fR, you may not be able to seek. See TQIODevice::isSequentialAccess() for more information.
The TQFile 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.
Reads bytes from the file into the char* \fIp\fR, until end-of-line or \fImaxlen\fR bytes have been read, whichever occurs first. Returns the number of bytes read, or -1 if there was an error. Any terminating newline is not stripped.
.PP
This function is only efficient for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Reads a line of text.
.PP
Reads bytes from the file into string \fIs\fR, until end-of-line or \fImaxlen\fR 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.
.PP
This function is only efficient for buffered files. Avoid using readLine() for files that have been opened with the IO_Raw flag.
.PP
Note that the string is read as plain Latin1 bytes, not Unicode.
Sets the name of the file to \fIname\fR. The name can have no path, a relative path or an absolute absolute path.
.PP
Do not call this function if the file has already been opened.
.PP
If the file name has no path or a relative path, the path used will be whatever the application's current directory path is \fIat the time of the open()\fR call.