File: qfile.3qt

package info (click to toggle)
qt-copy 2%3A3.0.3-20020329-1woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 77,328 kB
  • ctags: 59,132
  • sloc: cpp: 413,292; ansic: 88,042; sh: 10,673; yacc: 2,874; xml: 2,310; makefile: 466; lex: 453; perl: 422; sql: 29; lisp: 15
file content (507 lines) | stat: -rw-r--r-- 18,416 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
'\" t
.TH QFile 3qt "18 March 2002" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2001 Trolltech AS.  All rights reserved.  See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QFile \- I/O device that operates on files
.SH SYNOPSIS
\fC#include <qfile.h>\fR
.PP
Inherits 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 QCString (* \fBEncoderFn\fR ) ( const QString & fileName )"
.br
.ti -1c
.BI "typedef QString (* \fBDecoderFn\fR ) ( 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 m )"
.br
.ti -1c
.BI "bool \fBopen\fR ( int m, FILE * f )"
.br
.ti -1c
.BI "bool \fBopen\fR ( int m, int f )"
.br
.ti -1c
.BI "virtual void \fBclose\fR ()"
.br
.ti -1c
.BI "virtual void \fBflush\fR ()"
.br
.ti -1c
.BI "virtual Offset \fBsize\fR () const"
.br
.ti -1c
.BI "virtual Offset \fBat\fR () const"
.br
.ti -1c
.BI "virtual bool \fBat\fR ( Offset pos )"
.br
.ti -1c
.BI "virtual bool \fBatEnd\fR () const"
.br
.ti -1c
.BI "virtual Q_LONG \fBreadBlock\fR ( char * p, Q_ULONG len )"
.br
.ti -1c
.BI "virtual Q_LONG \fBreadLine\fR ( char * p, Q_ULONG maxlen )"
.br
.ti -1c
.BI "Q_LONG \fBreadLine\fR ( QString & s, Q_ULONG maxlen )"
.br
.ti -1c
.BI "virtual int \fBgetch\fR ()"
.br
.ti -1c
.BI "virtual int \fBputch\fR ( int ch )"
.br
.ti -1c
.BI "virtual int \fBungetch\fR ( int ch )"
.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 f )"
.br
.ti -1c
.BI "void \fBsetDecodingFunction\fR ( DecoderFn f )"
.br
.ti -1c
.BI "bool \fBexists\fR ( const QString & fileName )"
.br
.ti -1c
.BI "bool \fBremove\fR ( const QString & fileName )"
.br
.in -1c
.SS "Important Inherited Members"
.in +1c
.ti -1c
.BI "virtual QByteArray \fBreadAll\fR ()"
.br
.in -1c
.SH DESCRIPTION
The QFile class is an I/O device that operates on files.
.PP
QFile is an I/O device for reading and writing binary and text files. A QFile may be used by itself or more conveniently with a QDataStream or QTextStream.
.PP
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().
.PP
The file is opened with open(), closed with close() and flushed with flush(). Data is usually read and written using QDataStream or QTextStream, but you can read with readBlock() and readLine() and write with writeBlock(). QFile also supports getch(), ungetch() and putch().
.PP
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().
.PP
Here 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++, s.latin1() );
.br
        }
.br
        f.close();
.br
    }
.br
.fi
.PP
The QFileInfo class holds detailed information about a file, such as access permissions, file dates and file types.
.PP
The QDir class manages directories and lists of file names.
.PP
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.
.PP
See also QDataStream, QTextStream and Input/Output and Networking.
.SS "Member Type Documentation"
.SH "QFile::DecoderFn"
This is used by QFile::setDecodingFunction().
.SH "QFile::EncoderFn"
This is used by QFile::setEncodingFunction().
.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.
.PP
See also setName().
.SH "QFile::~QFile ()"
Destroys a QFile. Calls close().
.SH "bool QFile::at ( Offset pos )\fC [virtual]\fR"
Sets the file index to \fIpos\fR. Returns TRUE if successful; otherwise returns FALSE.
.PP
Example:
.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();
.br
.fi
.PP
Use at() without arguments to retrieve the file offset.
.PP
\fBWarning:\fR The result is undefined if the file was open()'ed using the IO_Append specifier.
.PP
See also size() and open().
.PP
Reimplemented from QIODevice.
.SH "Offset QFile::at () const\fC [virtual]\fR"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns the position in the file.
.PP
See also size().
.PP
Reimplemented from QIODevice.
.SH "bool QFile::atEnd () const\fC [virtual]\fR"
Returns TRUE if the end of file has been reached; otherwise returns FALSE.
.PP
See also size().
.PP
Reimplemented from QIODevice.
.SH "void QFile::close ()\fC [virtual]\fR"
Closes an open file.
.PP
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.
.PP
See also open() and flush().
.PP
Examples:
.)l addressbook/centralwidget.cpp, application/application.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, qdir/qdir.cpp, qwerty/qwerty.cpp and xml/outliner/outlinetree.cpp.
.PP
Reimplemented from QIODevice.
.SH "QString QFile::decodeName ( const QCString & localFileName )\fC [static]\fR"
This does the reverse of QFile::encodeName() using \fIlocalFileName\fR.
.PP
See also setDecodingFunction().
.SH "QCString QFile::encodeName ( const QString & fileName )\fC [static]\fR"
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, 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 at this time.
.PP
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 ware that such file names would probably then be unrecognizable when seen by other programs.
.PP
See also decodeName().
.SH "bool QFile::exists ( const QString & fileName )\fC [static]\fR"
Returns TRUE if the file given by \fIfileName\fR exists; otherwise returns FALSE.
.PP
Examples:
.)l dirview/dirview.cpp and helpviewer/helpwindow.cpp.
.SH "bool QFile::exists () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns TRUE if this file exists; otherwise returns FALSE.
.PP
See also name().
.SH "void QFile::flush ()\fC [virtual]\fR"
Flushes the file buffer to the disk.
.PP
close() also flushes the file buffer.
.PP
Reimplemented from QIODevice.
.SH "int QFile::getch ()\fC [virtual]\fR"
Reads a single byte/character from the file.
.PP
Returns the byte/character read, or -1 if the end of the file has been reached.
.PP
See also putch() and ungetch().
.PP
Reimplemented from QIODevice.
.SH "int QFile::handle () const"
Returns the file handle of the file.
.PP
This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(), as well as with QSocketNotifier.
.PP
If the file is not open or there is an error, handle() returns -1.
.PP
See also QSocketNotifier.
.SH "QString QFile::name () const"
Returns the name set by setName().
.PP
See 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.
.PP

.PP
The mode parameter \fIm\fR must be a combination of the following flags:
.TP
IO_Raw specified raw (non-buffered) file access.
.TP
IO_ReadOnly opens the file in read-only mode.
.TP
IO_WriteOnly opens the file in write-only mode (and truncates).
.TP
IO_ReadWrite opens the file in read/write mode, equivalent to \fC(IO_ReadOnly | IO_WriteOnly)\fR.
.TP
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.
.TP
IO_Truncate truncates the file.
.TP
IO_Translate enables carriage returns and linefeed translation for text files under MS-DOS, Windows and OS/2.
.PP
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.
.PP
\fBImportant:\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. seek(), 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.
.PP
Example:
.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 );
.br
.fi
.PP
See also name(), close(), isOpen() and flush().
.PP
Examples:
.)l action/application.cpp, application/application.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, qdir/qdir.cpp, qwerty/qwerty.cpp and xml/outliner/outlinetree.cpp.
.PP
Reimplemented from QIODevice.
.SH "bool QFile::open ( int m, FILE * f )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Opens a file in the mode \fIm\fR using an existing file handle \fIf\fR. Returns TRUE if successful, otherwise FALSE.
.PP
Example:
.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, qstrlen(msg) );      // write to stderr
.br
        f.close();
.br
    }
.br
.fi
.PP
When 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\fR, \fCstdout\fR, \fCstderr\fR, you may not be able to seek. See QIODevice::isSequentialAccess() for more information.
.PP
See also close().
.SH "bool QFile::open ( int m, int f )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Opens a file in the mode \fIm\fR using an existing file descriptor \fIf\fR. Returns TRUE if successful, otherwise FALSE.
.PP
When a QFile is opened using this function, close() does not actually close the file.
.PP
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.
.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).
.PP
See also close().
.SH "int QFile::putch ( int ch )\fC [virtual]\fR"
Writes the character \fIch\fR to the file.
.PP
Returns \fIch\fR, or -1 if some error occurred.
.PP
See also getch() and ungetch().
.PP
Reimplemented from QIODevice.
.SH "QByteArray QIODevice::readAll ()\fC [virtual]\fR"
This convenience function returns all of the remaining data in the device.
.SH "Q_LONG QFile::readBlock ( char * p, Q_ULONG len )\fC [virtual]\fR"
Reads at most \fIlen\fR bytes from the file into \fIp\fR and returns the number of bytes actually read.
.PP
Returns -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.
.PP
See also writeBlock().
.PP
Example: qwerty/qwerty.cpp.
.PP
Reimplemented from QIODevice.
.SH "Q_LONG QFile::readLine ( char * p, Q_ULONG maxlen )\fC [virtual]\fR"
Reads a line of text.
.PP
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. The terminating newline is not stripped.
.PP
This function is efficient only for buffered files. Avoid readLine() for files that have been opened with the IO_Raw flag.
.PP
See also readBlock() and QTextStream::readLine().
.PP
Reimplemented from QIODevice.
.SH "Q_LONG QFile::readLine ( QString & s, Q_ULONG maxlen )"
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.g. end of file. The terminating newline is not stripped.
.PP
This function is efficient only for buffered files. Avoid 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.
.PP
See also readBlock() and QTextStream::readLine().
.SH "bool QFile::remove ()"
Removes the file specified by the file name currently set. Returns TRUE if successful; otherwise returns FALSE.
.PP
The file is closed before it is removed.
.SH "bool QFile::remove ( const QString & fileName )\fC [static]\fR"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
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 file names to \fIf\fR. The default uses the locale-specific 8-bit encoding.
.PP
See also encodeName() and decodeName().
.SH "void QFile::setEncodingFunction ( EncoderFn f )\fC [static]\fR"
Sets the function for encoding Unicode file names to \fIf\fR. The default encodes in the locale-specific 8-bit encoding.
.PP
See also encodeName().
.SH "void QFile::setName ( const QString & name )"
Sets the name of the file to \fIname\fR. The name may have no path, a relative path or an absolute absolute directory 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.
.PP
Example:
.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
.br
.fi
.PP
Note that the directory separator "/" works for all operating systems supported by Qt.
.PP
See also name(), QFileInfo and QDir.
.SH "Offset QFile::size () const\fC [virtual]\fR"
Returns the file size.
.PP
See also at().
.PP
Example: table/statistics/statistics.cpp.
.PP
Reimplemented 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.
.PP
This function is normally called to "undo" a getch() operation.
.PP
Returns \fIch\fR, or -1 if some error occurred.
.PP
See also getch() and putch().
.PP
Reimplemented from QIODevice.

.SH "SEE ALSO"
.BR http://doc.trolltech.com/qfile.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com.  See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech. 
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (qfile.3qt) and the Qt
version (3.0.3).