1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qfile.cpp:77 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>QFile Class</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QFile Class Reference</h1>
|
---|
33 |
|
---|
34 | <p>The QFile class is an I/O device that operates on files.
|
---|
35 | <a href="#details">More...</a>
|
---|
36 | <p>Almost all the functions in this class are <a href="threads.html#reentrant">reentrant</a> when Qt is built with thread support. The exceptions are <a href="#setEncodingFunction"><b>setEncodingFunction</b></a>(), <a href="#setDecodingFunction"><b>setDecodingFunction</b></a>(), and <a href="#setErrorString"><b>setErrorString</b></a>().
|
---|
37 | </p><p><tt>#include <<a href="qfile-h.html">qfile.h</a>></tt>
|
---|
38 | <p>Inherits <a href="qiodevice.html">QIODevice</a>.
|
---|
39 | <p><a href="qfile-members.html">List of all member functions.</a>
|
---|
40 | <h2>Public Members</h2>
|
---|
41 | <ul>
|
---|
42 | <li class=fn><a href="#QFile"><b>QFile</b></a> ()</li>
|
---|
43 | <li class=fn><a href="#QFile-2"><b>QFile</b></a> ( const QString & name )</li>
|
---|
44 | <li class=fn><a href="#~QFile"><b>~QFile</b></a> ()</li>
|
---|
45 | <li class=fn>QString <a href="#name"><b>name</b></a> () const</li>
|
---|
46 | <li class=fn>void <a href="#setName"><b>setName</b></a> ( const QString & name )</li>
|
---|
47 | <li class=fn>typedef QCString (* <a href="#EncoderFn"><b>EncoderFn</b></a> ) ( const QString & fileName )</li>
|
---|
48 | <li class=fn>typedef QString (* <a href="#DecoderFn"><b>DecoderFn</b></a> ) ( const QCString & localfileName )</li>
|
---|
49 | <li class=fn>bool <a href="#exists-2"><b>exists</b></a> () const</li>
|
---|
50 | <li class=fn>bool <a href="#remove"><b>remove</b></a> ()</li>
|
---|
51 | <li class=fn>virtual bool <a href="#open"><b>open</b></a> ( int m )</li>
|
---|
52 | <li class=fn>bool <a href="#open-2"><b>open</b></a> ( int m, FILE * f )</li>
|
---|
53 | <li class=fn>bool <a href="#open-3"><b>open</b></a> ( int m, int f )</li>
|
---|
54 | <li class=fn>virtual void <a href="#close"><b>close</b></a> ()</li>
|
---|
55 | <li class=fn>virtual void <a href="#flush"><b>flush</b></a> ()</li>
|
---|
56 | <li class=fn>virtual Offset <a href="#size"><b>size</b></a> () const</li>
|
---|
57 | <li class=fn>virtual bool <a href="#atEnd"><b>atEnd</b></a> () const</li>
|
---|
58 | <li class=fn>virtual Q_LONG <a href="#readLine"><b>readLine</b></a> ( char * p, Q_ULONG maxlen )</li>
|
---|
59 | <li class=fn>Q_LONG <a href="#readLine-2"><b>readLine</b></a> ( QString & s, Q_ULONG maxlen )</li>
|
---|
60 | <li class=fn>virtual int <a href="#getch"><b>getch</b></a> ()</li>
|
---|
61 | <li class=fn>virtual int <a href="#putch"><b>putch</b></a> ( int ch )</li>
|
---|
62 | <li class=fn>virtual int <a href="#ungetch"><b>ungetch</b></a> ( int ch )</li>
|
---|
63 | <li class=fn>int <a href="#handle"><b>handle</b></a> () const</li>
|
---|
64 | <li class=fn>QString <a href="#errorString"><b>errorString</b></a> () const</li>
|
---|
65 | </ul>
|
---|
66 | <h2>Static Public Members</h2>
|
---|
67 | <ul>
|
---|
68 | <li class=fn>QCString <a href="#encodeName"><b>encodeName</b></a> ( const QString & fileName )</li>
|
---|
69 | <li class=fn>QString <a href="#decodeName"><b>decodeName</b></a> ( const QCString & localFileName )</li>
|
---|
70 | <li class=fn>void <a href="#setEncodingFunction"><b>setEncodingFunction</b></a> ( EncoderFn f )</li>
|
---|
71 | <li class=fn>void <a href="#setDecodingFunction"><b>setDecodingFunction</b></a> ( DecoderFn f )</li>
|
---|
72 | <li class=fn>bool <a href="#exists"><b>exists</b></a> ( const QString & fileName )</li>
|
---|
73 | <li class=fn>bool <a href="#remove-2"><b>remove</b></a> ( const QString & fileName )</li>
|
---|
74 | </ul>
|
---|
75 | <h2>Important Inherited Members</h2>
|
---|
76 | <ul>
|
---|
77 | <li class=fn>virtual QByteArray <a href="#readAll"><b>readAll</b></a> ()</li>
|
---|
78 | </ul>
|
---|
79 | <h2>Protected Members</h2>
|
---|
80 | <ul>
|
---|
81 | <li class=fn>void <a href="#setErrorString"><b>setErrorString</b></a> ( const QString & str )</li>
|
---|
82 | </ul>
|
---|
83 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | The QFile class is an I/O device that operates on files.
|
---|
88 | <p>
|
---|
89 |
|
---|
90 | <p> QFile is an I/O device for reading and writing binary and text
|
---|
91 | files. A QFile may be used by itself or more conveniently with a
|
---|
92 | <a href="qdatastream.html">QDataStream</a> or <a href="qtextstream.html">QTextStream</a>.
|
---|
93 | <p> The file name is usually passed in the constructor but can be
|
---|
94 | changed with <a href="#setName">setName</a>(). You can check for a file's existence with
|
---|
95 | <a href="#exists">exists</a>() and remove a file with <a href="#remove">remove</a>().
|
---|
96 | <p> The file is opened with <a href="#open">open</a>(), closed with <a href="#close">close</a>() and flushed
|
---|
97 | with <a href="#flush">flush</a>(). Data is usually read and written using QDataStream
|
---|
98 | or QTextStream, but you can read with <a href="qiodevice.html#readBlock">readBlock</a>() and <a href="#readLine">readLine</a>()
|
---|
99 | and write with <a href="qiodevice.html#writeBlock">writeBlock</a>(). QFile also supports <a href="#getch">getch</a>(),
|
---|
100 | <a href="#ungetch">ungetch</a>() and <a href="#putch">putch</a>().
|
---|
101 | <p> The size of the file is returned by <a href="#size">size</a>(). You can get the
|
---|
102 | current file position or move to a new file position using the
|
---|
103 | <a href="qiodevice.html#at">at</a>() functions. If you've reached the end of the file, <a href="#atEnd">atEnd</a>()
|
---|
104 | returns TRUE. The file handle is returned by <a href="#handle">handle</a>().
|
---|
105 | <p> Here is a code fragment that uses <a href="qtextstream.html">QTextStream</a> to read a text file
|
---|
106 | line by line. It prints each line with a line number.
|
---|
107 | <pre>
|
---|
108 | <a href="qstringlist.html">QStringList</a> lines;
|
---|
109 | QFile file( "file.txt" );
|
---|
110 | if ( file.<a href="#open">open</a>( IO_ReadOnly ) ) {
|
---|
111 | <a href="qtextstream.html">QTextStream</a> stream( &file );
|
---|
112 | <a href="qstring.html">QString</a> line;
|
---|
113 | int i = 1;
|
---|
114 | while ( !stream.<a href="qtextstream.html#atEnd">atEnd</a>() ) {
|
---|
115 | line = stream.<a href="qtextstream.html#readLine">readLine</a>(); // line of text excluding '\n'
|
---|
116 | printf( "%3d: %s\n", i++, line.<a href="qstring.html#latin1">latin1</a>() );
|
---|
117 | lines += line;
|
---|
118 | }
|
---|
119 | file.<a href="#close">close</a>();
|
---|
120 | }
|
---|
121 | </pre>
|
---|
122 |
|
---|
123 | <p> Writing text is just as easy. The following example shows how to
|
---|
124 | write the data we read into the string list from the previous
|
---|
125 | example:
|
---|
126 | <pre>
|
---|
127 | QFile file( "file.txt" );
|
---|
128 | if ( file.<a href="#open">open</a>( IO_WriteOnly ) ) {
|
---|
129 | <a href="qtextstream.html">QTextStream</a> stream( &file );
|
---|
130 | for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
|
---|
131 | stream << *it << "\n";
|
---|
132 | file.<a href="#close">close</a>();
|
---|
133 | }
|
---|
134 | </pre>
|
---|
135 |
|
---|
136 | <p> The <a href="qfileinfo.html">QFileInfo</a> class holds detailed information about a file, such
|
---|
137 | as access permissions, file dates and file types.
|
---|
138 | <p> The <a href="qdir.html">QDir</a> class manages directories and lists of file names.
|
---|
139 | <p> Qt uses Unicode file names. If you want to do your own I/O on Unix
|
---|
140 | systems you may want to use <a href="#encodeName">encodeName</a>() (and <a href="#decodeName">decodeName</a>()) to
|
---|
141 | convert the file name into the local encoding.
|
---|
142 | <p>
|
---|
143 | <p> <p>See also <a href="qdatastream.html">QDataStream</a>, <a href="qtextstream.html">QTextStream</a>, and <a href="io.html">Input/Output and Networking</a>.
|
---|
144 |
|
---|
145 | <hr><h2>Member Type Documentation</h2>
|
---|
146 | <h3 class=fn><a name="DecoderFn"></a>QFile::DecoderFn</h3>
|
---|
147 |
|
---|
148 | <p> This is used by <a href="#setDecodingFunction">QFile::setDecodingFunction</a>().
|
---|
149 |
|
---|
150 | <h3 class=fn><a name="EncoderFn"></a>QFile::EncoderFn</h3>
|
---|
151 |
|
---|
152 | <p> This is used by <a href="#setEncodingFunction">QFile::setEncodingFunction</a>().
|
---|
153 |
|
---|
154 | <hr><h2>Member Function Documentation</h2>
|
---|
155 | <h3 class=fn><a name="QFile"></a>QFile::QFile ()
|
---|
156 | </h3>
|
---|
157 | Constructs a QFile with no name.
|
---|
158 |
|
---|
159 | <h3 class=fn><a name="QFile-2"></a>QFile::QFile ( const <a href="qstring.html">QString</a> & name )
|
---|
160 | </h3>
|
---|
161 | Constructs a QFile with a file name <em>name</em>.
|
---|
162 | <p> <p>See also <a href="#setName">setName</a>().
|
---|
163 |
|
---|
164 | <h3 class=fn><a name="~QFile"></a>QFile::~QFile ()
|
---|
165 | </h3>
|
---|
166 | Destroys a QFile. Calls <a href="#close">close</a>().
|
---|
167 |
|
---|
168 | <h3 class=fn>bool <a name="atEnd"></a>QFile::atEnd () const<tt> [virtual]</tt>
|
---|
169 | </h3>
|
---|
170 | Returns TRUE if the end of file has been reached; otherwise returns FALSE.
|
---|
171 | If QFile has not been <a href="#open">open</a>()'d, then the behavior is undefined.
|
---|
172 | <p> <p>See also <a href="#size">size</a>().
|
---|
173 |
|
---|
174 | <p>Example: <a href="distributor-example.html#x2655">distributor/distributor.ui.h</a>.
|
---|
175 | <p>Reimplemented from <a href="qiodevice.html#atEnd">QIODevice</a>.
|
---|
176 | <h3 class=fn>void <a name="close"></a>QFile::close ()<tt> [virtual]</tt>
|
---|
177 | </h3>
|
---|
178 | Closes an open file.
|
---|
179 | <p> The file is not closed if it was opened with an existing file handle.
|
---|
180 | If the existing file handle is a <tt>FILE*</tt>, the file is flushed.
|
---|
181 | If the existing file handle is an <tt>int</tt> file descriptor, nothing
|
---|
182 | is done to the file.
|
---|
183 | <p> Some "write-behind" filesystems may report an unspecified error on
|
---|
184 | closing the file. These errors only indicate that something may
|
---|
185 | have gone wrong since the previous <a href="#open">open</a>(). In such a case <a href="qiodevice.html#status">status</a>()
|
---|
186 | reports <a href="qiodevice.html#status">IO_UnspecifiedError</a> after <a href="#close">close</a>(), otherwise <a href="qiodevice.html#status">IO_Ok</a>.
|
---|
187 | <p> <p>See also <a href="#open">open</a>() and <a href="#flush">flush</a>().
|
---|
188 |
|
---|
189 | <p>Examples: <a href="tutorial2-07.html#x2590">chart/chartform_files.cpp</a>, <a href="distributor-example.html#x2656">distributor/distributor.ui.h</a>, <a href="helpviewer-example.html#x987">helpviewer/helpwindow.cpp</a>, <a href="mdi-example.html#x2023">mdi/application.cpp</a>, <a href="qdir-example.html#x1814">qdir/qdir.cpp</a>, <a href="qwerty-example.html#x362">qwerty/qwerty.cpp</a>, and <a href="outliner-example.html#x1914">xml/outliner/outlinetree.cpp</a>.
|
---|
190 | <p>Reimplemented from <a href="qiodevice.html#close">QIODevice</a>.
|
---|
191 | <h3 class=fn><a href="qstring.html">QString</a> <a name="decodeName"></a>QFile::decodeName ( const <a href="qcstring.html">QCString</a> & localFileName )<tt> [static]</tt>
|
---|
192 | </h3>
|
---|
193 | This does the reverse of <a href="#encodeName">QFile::encodeName</a>() using <em>localFileName</em>.
|
---|
194 | <p> <p>See also <a href="#setDecodingFunction">setDecodingFunction</a>().
|
---|
195 |
|
---|
196 | <p>Example: <a href="distributor-example.html#x2657">distributor/distributor.ui.h</a>.
|
---|
197 | <h3 class=fn><a href="qcstring.html">QCString</a> <a name="encodeName"></a>QFile::encodeName ( const <a href="qstring.html">QString</a> & fileName )<tt> [static]</tt>
|
---|
198 | </h3>
|
---|
199 | When you use QFile, <a href="qfileinfo.html">QFileInfo</a>, and <a href="qdir.html">QDir</a> to access the file system
|
---|
200 | with Qt, you can use Unicode file names. On Unix, these file names
|
---|
201 | are converted to an 8-bit encoding. If you want to do your own
|
---|
202 | file I/O on Unix, you should convert the file name using this
|
---|
203 | function. On Windows NT/2000, Unicode file names are supported
|
---|
204 | directly in the file system and this function should be avoided.
|
---|
205 | On Windows 95, non-Latin1 locales are not supported.
|
---|
206 | <p> By default, this function converts <em>fileName</em> to the local 8-bit
|
---|
207 | encoding determined by the user's locale. This is sufficient for
|
---|
208 | file names that the user chooses. File names hard-coded into the
|
---|
209 | application should only use 7-bit ASCII filename characters.
|
---|
210 | <p> The conversion scheme can be changed using <a href="#setEncodingFunction">setEncodingFunction</a>().
|
---|
211 | This might be useful if you wish to give the user an option to
|
---|
212 | store file names in UTF-8, etc., but be aware that such file names
|
---|
213 | would probably then be unrecognizable when seen by other programs.
|
---|
214 | <p> <p>See also <a href="#decodeName">decodeName</a>().
|
---|
215 |
|
---|
216 | <p>Example: <a href="distributor-example.html#x2658">distributor/distributor.ui.h</a>.
|
---|
217 | <h3 class=fn><a href="qstring.html">QString</a> <a name="errorString"></a>QFile::errorString () const
|
---|
218 | </h3>
|
---|
219 | Returns a human-readable description of the reason of an error that occurred
|
---|
220 | on the device. The error described by the string corresponds to changes of
|
---|
221 | <a href="qiodevice.html#status">QIODevice::status</a>(). If the status is reset, the error string is also reset.
|
---|
222 | <p> The returned strings are not translated with the <a href="qobject.html#tr">QObject::tr</a>() or
|
---|
223 | <a href="qapplication.html#translate">QApplication::translate</a>() functions. They are marked as translatable
|
---|
224 | strings in the "QFile" context. Before you show the string to the user you
|
---|
225 | should translate it first, for example:
|
---|
226 | <p> <pre>
|
---|
227 | QFile f( "address.dat" );
|
---|
228 | if ( !f.<a href="#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) {
|
---|
229 | QMessageBox::<a href="qmessagebox.html#critical">critical</a>(
|
---|
230 | this,
|
---|
231 | tr("Open failed"),
|
---|
232 | tr("Could not open file for reading: %1").arg( qApp-><a href="qapplication.html#translate">translate</a>("QFile",f.<a href="#errorString">errorString</a>()) )
|
---|
233 | );
|
---|
234 | return;
|
---|
235 | }
|
---|
236 | </pre>
|
---|
237 |
|
---|
238 | <p> <p>See also <a href="qiodevice.html#status">QIODevice::status</a>(), <a href="qiodevice.html#resetStatus">QIODevice::resetStatus</a>(), and <a href="#setErrorString">setErrorString</a>().
|
---|
239 |
|
---|
240 | <h3 class=fn>bool <a name="exists"></a>QFile::exists ( const <a href="qstring.html">QString</a> & fileName )<tt> [static]</tt>
|
---|
241 | </h3>
|
---|
242 | Returns TRUE if the file given by <em>fileName</em> exists; otherwise
|
---|
243 | returns FALSE.
|
---|
244 |
|
---|
245 | <p>Examples: <a href="tutorial2-05.html#x2563">chart/chartform.cpp</a>, <a href="dirview-example.html#x1674">dirview/dirview.cpp</a>, and <a href="helpviewer-example.html#x988">helpviewer/helpwindow.cpp</a>.
|
---|
246 | <h3 class=fn>bool <a name="exists-2"></a>QFile::exists () const
|
---|
247 | </h3>
|
---|
248 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
249 | <p> Returns TRUE if this file exists; otherwise returns FALSE.
|
---|
250 | <p> <p>See also <a href="#name">name</a>().
|
---|
251 |
|
---|
252 | <h3 class=fn>void <a name="flush"></a>QFile::flush ()<tt> [virtual]</tt>
|
---|
253 | </h3>
|
---|
254 | Flushes the file buffer to the disk.
|
---|
255 | <p> <a href="#close">close</a>() also flushes the file buffer.
|
---|
256 |
|
---|
257 | <p>Reimplemented from <a href="qiodevice.html#flush">QIODevice</a>.
|
---|
258 | <h3 class=fn>int <a name="getch"></a>QFile::getch ()<tt> [virtual]</tt>
|
---|
259 | </h3>
|
---|
260 | Reads a single byte/character from the file.
|
---|
261 | <p> Returns the byte/character read, or -1 if the end of the file has
|
---|
262 | been reached.
|
---|
263 | <p> <p>See also <a href="#putch">putch</a>() and <a href="#ungetch">ungetch</a>().
|
---|
264 |
|
---|
265 | <p>Reimplemented from <a href="qiodevice.html#getch">QIODevice</a>.
|
---|
266 | <h3 class=fn>int <a name="handle"></a>QFile::handle () const
|
---|
267 | </h3>
|
---|
268 | Returns the file handle of the file.
|
---|
269 | <p> This is a small positive integer, suitable for use with C library
|
---|
270 | functions such as fdopen() and fcntl(). On systems that use file
|
---|
271 | descriptors for sockets (ie. Unix systems, but not Windows) the handle
|
---|
272 | can be used with <a href="qsocketnotifier.html">QSocketNotifier</a> as well.
|
---|
273 | <p> If the file is not open or there is an error, <a href="#handle">handle</a>() returns -1.
|
---|
274 | <p> <p>See also <a href="qsocketnotifier.html">QSocketNotifier</a>.
|
---|
275 |
|
---|
276 | <h3 class=fn><a href="qstring.html">QString</a> <a name="name"></a>QFile::name () const
|
---|
277 | </h3>
|
---|
278 |
|
---|
279 | <p> Returns the name set by <a href="#setName">setName</a>().
|
---|
280 | <p> <p>See also <a href="#setName">setName</a>() and <a href="qfileinfo.html#fileName">QFileInfo::fileName</a>().
|
---|
281 |
|
---|
282 | <h3 class=fn>bool <a name="open"></a>QFile::open ( int m )<tt> [virtual]</tt>
|
---|
283 | </h3>
|
---|
284 | Opens the file specified by the file name currently set, using the
|
---|
285 | mode <em>m</em>. Returns TRUE if successful, otherwise FALSE.
|
---|
286 | <p> <!-- index IO_Raw --><!-- index IO_ReadOnly --><!-- index IO_WriteOnly --><!-- index IO_ReadWrite --><!-- index IO_Append --><!-- index IO_Truncate --><!-- index IO_Translate -->
|
---|
287 | <p> The mode parameter <em>m</em> must be a combination of the following flags:
|
---|
288 | <center><table cellpadding="4" cellspacing="2" border="0">
|
---|
289 | <tr bgcolor="#a2c511"> <th valign="top">Flag <th valign="top">Meaning
|
---|
290 | <tr bgcolor="#f0f0f0"> <td valign="top">IO_Raw
|
---|
291 | <td valign="top">Raw (non-buffered) file access.
|
---|
292 | <tr bgcolor="#d0d0d0"> <td valign="top">IO_ReadOnly
|
---|
293 | <td valign="top">Opens the file in read-only mode.
|
---|
294 | <tr bgcolor="#f0f0f0"> <td valign="top">IO_WriteOnly
|
---|
295 | <td valign="top">Opens the file in write-only mode. If this flag is used
|
---|
296 | with another flag, e.g. <a href="qfile.html#open">IO_ReadOnly</a> or <a href="qfile.html#open">IO_Raw</a> or <a href="qfile.html#open">IO_Append</a>, the file is <em>not</em> truncated; but if used on
|
---|
297 | its own (or with <a href="qfile.html#open">IO_Truncate</a>), the file is truncated.
|
---|
298 | <tr bgcolor="#d0d0d0"> <td valign="top">IO_ReadWrite
|
---|
299 | <td valign="top">Opens the file in read/write mode, equivalent to <tt>(IO_ReadOnly | IO_WriteOnly)</tt>.
|
---|
300 | <tr bgcolor="#f0f0f0"> <td valign="top">IO_Append
|
---|
301 | <td valign="top">Opens the file in append mode. (You must actually use <tt>(IO_WriteOnly | IO_Append)</tt> to make the file writable and
|
---|
302 | to go into append mode.) This mode is very useful when you
|
---|
303 | want to write something to a log file. The file index is
|
---|
304 | set to the end of the file. Note that the result is
|
---|
305 | undefined if you position the file index manually using
|
---|
306 | <a href="qiodevice.html#at">at</a>() in append mode.
|
---|
307 | <tr bgcolor="#d0d0d0"> <td valign="top">IO_Truncate
|
---|
308 | <td valign="top">Truncates the file.
|
---|
309 | <tr bgcolor="#f0f0f0"> <td valign="top">IO_Translate
|
---|
310 | <td valign="top">Enables carriage returns and linefeed translation for text
|
---|
311 | files under Windows.
|
---|
312 | </table></center>
|
---|
313 | <p> The raw access mode is best when I/O is block-operated using a 4KB
|
---|
314 | block size or greater. Buffered access works better when reading
|
---|
315 | small portions of data at a time.
|
---|
316 | <p> <b>Warning:</b> When working with buffered files, data may not be written
|
---|
317 | to the file at once. Call <a href="#flush">flush</a>() to make sure that the data is
|
---|
318 | really written.
|
---|
319 | <p> <b>Warning:</b> If you have a buffered file opened for both reading and
|
---|
320 | writing you must not perform an input operation immediately after
|
---|
321 | an output operation or vice versa. You should always call flush()
|
---|
322 | or a file positioning operation, e.g. at(), between input and
|
---|
323 | output operations, otherwise the buffer may contain garbage.
|
---|
324 | <p> If the file does not exist and <a href="qfile.html#open">IO_WriteOnly</a> or <a href="qfile.html#open">IO_ReadWrite</a>
|
---|
325 | is specified, it is created.
|
---|
326 | <p> Example:
|
---|
327 | <pre>
|
---|
328 | QFile f1( "/tmp/data.bin" );
|
---|
329 | f1.<a href="#open">open</a>( IO_Raw | IO_ReadWrite );
|
---|
330 |
|
---|
331 | QFile f2( "readme.txt" );
|
---|
332 | f2.<a href="#open">open</a>( IO_ReadOnly | IO_Translate );
|
---|
333 |
|
---|
334 | QFile f3( "audit.log" );
|
---|
335 | f3.<a href="#open">open</a>( IO_WriteOnly | IO_Append );
|
---|
336 | </pre>
|
---|
337 |
|
---|
338 | <p> <p>See also <a href="#name">name</a>(), <a href="#close">close</a>(), <a href="qiodevice.html#isOpen">isOpen</a>(), and <a href="#flush">flush</a>().
|
---|
339 |
|
---|
340 | <p>Examples: <a href="simple-application-example.html#x1548">application/application.cpp</a>, <a href="tutorial2-07.html#x2591">chart/chartform_files.cpp</a>, <a href="distributor-example.html#x2659">distributor/distributor.ui.h</a>, <a href="helpviewer-example.html#x989">helpviewer/helpwindow.cpp</a>, <a href="qdir-example.html#x1815">qdir/qdir.cpp</a>, <a href="qwerty-example.html#x363">qwerty/qwerty.cpp</a>, and <a href="outliner-example.html#x1915">xml/outliner/outlinetree.cpp</a>.
|
---|
341 | <p>Reimplemented from <a href="qiodevice.html#open">QIODevice</a>.
|
---|
342 | <h3 class=fn>bool <a name="open-2"></a>QFile::open ( int m, FILE * f )
|
---|
343 | </h3>
|
---|
344 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
345 | <p> Opens a file in the mode <em>m</em> using an existing file handle <em>f</em>.
|
---|
346 | Returns TRUE if successful, otherwise FALSE.
|
---|
347 | <p> Example:
|
---|
348 | <pre>
|
---|
349 | #include <stdio.h>
|
---|
350 |
|
---|
351 | void printError( const char* msg )
|
---|
352 | {
|
---|
353 | QFile f;
|
---|
354 | f.<a href="#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a>, stderr );
|
---|
355 | f.<a href="qiodevice.html#writeBlock">writeBlock</a>( msg, qstrlen(msg) ); // write to stderr
|
---|
356 | f.<a href="#close">close</a>();
|
---|
357 | }
|
---|
358 | </pre>
|
---|
359 |
|
---|
360 | <p> When a QFile is opened using this function, <a href="#close">close</a>() does not actually
|
---|
361 | close the file, only flushes it.
|
---|
362 | <p> <b>Warning:</b> If <em>f</em> is <tt>stdin</tt>, <tt>stdout</tt>, <tt>stderr</tt>, you may not
|
---|
363 | be able to seek. See <a href="qiodevice.html#isSequentialAccess">QIODevice::isSequentialAccess</a>() for more
|
---|
364 | information.
|
---|
365 | <p> <p>See also <a href="#close">close</a>().
|
---|
366 |
|
---|
367 | <h3 class=fn>bool <a name="open-3"></a>QFile::open ( int m, int f )
|
---|
368 | </h3>
|
---|
369 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
370 | <p> Opens a file in the mode <em>m</em> using an existing file descriptor <em>f</em>.
|
---|
371 | Returns TRUE if successful, otherwise FALSE.
|
---|
372 | <p> When a QFile is opened using this function, <a href="#close">close</a>() does not actually
|
---|
373 | close the file.
|
---|
374 | <p> The QFile that is opened using this function, is automatically set to be in
|
---|
375 | raw mode; this means that the file input/output functions are slow. If you
|
---|
376 | run into performance issues, you should try to use one of the other open
|
---|
377 | functions.
|
---|
378 | <p> <b>Warning:</b> If <em>f</em> is one of 0 (stdin), 1 (stdout) or 2 (stderr), you may not
|
---|
379 | be able to seek. <a href="#size">size</a>() is set to <tt>INT_MAX</tt> (in limits.h).
|
---|
380 | <p> <p>See also <a href="#close">close</a>().
|
---|
381 |
|
---|
382 | <h3 class=fn>int <a name="putch"></a>QFile::putch ( int ch )<tt> [virtual]</tt>
|
---|
383 | </h3>
|
---|
384 | Writes the character <em>ch</em> to the file.
|
---|
385 | <p> Returns <em>ch</em>, or -1 if some error occurred.
|
---|
386 | <p> <p>See also <a href="#getch">getch</a>() and <a href="#ungetch">ungetch</a>().
|
---|
387 |
|
---|
388 | <p>Reimplemented from <a href="qiodevice.html#putch">QIODevice</a>.
|
---|
389 | <h3 class=fn><a href="qbytearray.html">QByteArray</a> <a name="readAll"></a>QIODevice::readAll ()<tt> [virtual]</tt>
|
---|
390 | </h3>
|
---|
391 | This convenience function returns all of the remaining data in the
|
---|
392 | device.
|
---|
393 |
|
---|
394 | <h3 class=fn>Q_LONG <a name="readLine"></a>QFile::readLine ( char * p, Q_ULONG maxlen )<tt> [virtual]</tt>
|
---|
395 | </h3>
|
---|
396 | Reads a line of text.
|
---|
397 | <p> Reads bytes from the file into the char* <em>p</em>, until end-of-line
|
---|
398 | or <em>maxlen</em> bytes have been read, whichever occurs first. Returns
|
---|
399 | the number of bytes read, or -1 if there was an error. Any
|
---|
400 | terminating newline is not stripped.
|
---|
401 | <p> This function is only efficient for buffered files. Avoid
|
---|
402 | <a href="#readLine">readLine</a>() for files that have been opened with the <a href="qfile.html#open">IO_Raw</a>
|
---|
403 | flag.
|
---|
404 | <p> <p>See also <a href="qiodevice.html#readBlock">readBlock</a>() and <a href="qtextstream.html#readLine">QTextStream::readLine</a>().
|
---|
405 |
|
---|
406 | <p>Reimplemented from <a href="qiodevice.html#readLine">QIODevice</a>.
|
---|
407 | <h3 class=fn>Q_LONG <a name="readLine-2"></a>QFile::readLine ( <a href="qstring.html">QString</a> & s, Q_ULONG maxlen )
|
---|
408 | </h3>
|
---|
409 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
410 | <p> Reads a line of text.
|
---|
411 | <p> Reads bytes from the file into string <em>s</em>, until end-of-line or
|
---|
412 | <em>maxlen</em> bytes have been read, whichever occurs first. Returns
|
---|
413 | the number of bytes read, or -1 if there was an error, e.g. end of
|
---|
414 | file. Any terminating newline is not stripped.
|
---|
415 | <p> This function is only efficient for buffered files. Avoid using
|
---|
416 | <a href="#readLine">readLine</a>() for files that have been opened with the <a href="qfile.html#open">IO_Raw</a>
|
---|
417 | flag.
|
---|
418 | <p> Note that the string is read as plain Latin1 bytes, not Unicode.
|
---|
419 | <p> <p>See also <a href="qiodevice.html#readBlock">readBlock</a>() and <a href="qtextstream.html#readLine">QTextStream::readLine</a>().
|
---|
420 |
|
---|
421 | <h3 class=fn>bool <a name="remove"></a>QFile::remove ()
|
---|
422 | </h3>
|
---|
423 | Removes the file specified by the file name currently set. Returns
|
---|
424 | TRUE if successful; otherwise returns FALSE.
|
---|
425 | <p> The file is closed before it is removed.
|
---|
426 |
|
---|
427 | <h3 class=fn>bool <a name="remove-2"></a>QFile::remove ( const <a href="qstring.html">QString</a> & fileName )<tt> [static]</tt>
|
---|
428 | </h3>
|
---|
429 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
430 | <p> Removes the file <em>fileName</em>.
|
---|
431 | Returns TRUE if successful, otherwise FALSE.
|
---|
432 |
|
---|
433 | <h3 class=fn>void <a name="setDecodingFunction"></a>QFile::setDecodingFunction ( <a href="qfile.html#DecoderFn">DecoderFn</a> f )<tt> [static]</tt>
|
---|
434 | </h3><p><b>Warning:</b> This function is <i>not</i> <a href="threads.html#reentrant">reentrant</a>.</p>
|
---|
435 |
|
---|
436 |
|
---|
437 | <p> Sets the function for decoding 8-bit file names to <em>f</em>. The
|
---|
438 | default uses the locale-specific 8-bit encoding.
|
---|
439 | <p> <p>See also <a href="#encodeName">encodeName</a>() and <a href="#decodeName">decodeName</a>().
|
---|
440 |
|
---|
441 | <h3 class=fn>void <a name="setEncodingFunction"></a>QFile::setEncodingFunction ( <a href="qfile.html#EncoderFn">EncoderFn</a> f )<tt> [static]</tt>
|
---|
442 | </h3><p><b>Warning:</b> This function is <i>not</i> <a href="threads.html#reentrant">reentrant</a>.</p>
|
---|
443 |
|
---|
444 |
|
---|
445 | <p> Sets the function for encoding Unicode file names to <em>f</em>. The
|
---|
446 | default encodes in the locale-specific 8-bit encoding.
|
---|
447 | <p> <p>See also <a href="#encodeName">encodeName</a>().
|
---|
448 |
|
---|
449 | <h3 class=fn>void <a name="setErrorString"></a>QFile::setErrorString ( const <a href="qstring.html">QString</a> & str )<tt> [protected]</tt>
|
---|
450 | </h3><p><b>Warning:</b> This function is <i>not</i> <a href="threads.html#reentrant">reentrant</a>.</p>
|
---|
451 |
|
---|
452 |
|
---|
453 | <p> Sets the error string returned by the <a href="#errorString">errorString</a>() function to <em>str</em>.
|
---|
454 | <p> <p>See also <a href="#errorString">errorString</a>() and <a href="qiodevice.html#status">QIODevice::status</a>().
|
---|
455 |
|
---|
456 | <h3 class=fn>void <a name="setName"></a>QFile::setName ( const <a href="qstring.html">QString</a> & name )
|
---|
457 | </h3>
|
---|
458 | Sets the name of the file to <em>name</em>. The name can have no path, a
|
---|
459 | relative path or an absolute absolute path.
|
---|
460 | <p> Do not call this function if the file has already been opened.
|
---|
461 | <p> If the file name has no path or a relative path, the path used
|
---|
462 | will be whatever the application's current directory path is
|
---|
463 | <em>at the time of the <a href="#open">open</a>()</em> call.
|
---|
464 | <p> Example:
|
---|
465 | <pre>
|
---|
466 | QFile file;
|
---|
467 | QDir::<a href="qdir.html#setCurrent">setCurrent</a>( "/tmp" );
|
---|
468 | file.<a href="#setName">setName</a>( "readme.txt" );
|
---|
469 | QDir::<a href="qdir.html#setCurrent">setCurrent</a>( "/home" );
|
---|
470 | file.<a href="#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ); // opens "/home/readme.txt" under Unix
|
---|
471 | </pre>
|
---|
472 |
|
---|
473 | <p> Note that the directory separator "/" works for all operating
|
---|
474 | systems supported by Qt.
|
---|
475 | <p> <p>See also <a href="#name">name</a>(), <a href="qfileinfo.html">QFileInfo</a>, and <a href="qdir.html">QDir</a>.
|
---|
476 |
|
---|
477 | <h3 class=fn><a href="qiodevice.html#Offset">Offset</a> <a name="size"></a>QFile::size () const<tt> [virtual]</tt>
|
---|
478 | </h3>
|
---|
479 | Returns the file size.
|
---|
480 | <p>See also <a href="qiodevice.html#at">at</a>().
|
---|
481 |
|
---|
482 | <p>Example: <a href="statistics-example.html#x2781">table/statistics/statistics.cpp</a>.
|
---|
483 | <p>Reimplemented from <a href="qiodevice.html#size">QIODevice</a>.
|
---|
484 | <h3 class=fn>int <a name="ungetch"></a>QFile::ungetch ( int ch )<tt> [virtual]</tt>
|
---|
485 | </h3>
|
---|
486 | Puts the character <em>ch</em> back into the file and decrements the
|
---|
487 | index if it is not zero.
|
---|
488 | <p> This function is normally called to "undo" a <a href="#getch">getch</a>() operation.
|
---|
489 | <p> Returns <em>ch</em>, or -1 if an error occurred.
|
---|
490 | <p> <p>See also <a href="#getch">getch</a>() and <a href="#putch">putch</a>().
|
---|
491 |
|
---|
492 | <p>Reimplemented from <a href="qiodevice.html#ungetch">QIODevice</a>.
|
---|
493 | <!-- eof -->
|
---|
494 | <hr><p>
|
---|
495 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
496 | Copyright © 1995-2007
|
---|
497 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
---|
498 | <table width=100% cellspacing=0 border=0><tr>
|
---|
499 | <td>Copyright © 2007
|
---|
500 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
501 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
502 | </table></div></address></body>
|
---|
503 | </html>
|
---|