📄 fil_2309.htm
字号:
Truncate file; i.e., delete file content<BR></TD></TR><TR VALIGN=top><TD><SAMP>ios_base::binary</SAMP><BR></TD><TD>Binary mode<BR></TD></TR></TABLE></CENTER><A NAME="The <SAMP>in</SAMP> and <SAMP>out</SAMP> Open Modes"><H3>The <SAMP>in</SAMP> and <SAMP>out</SAMP> Open Modes</H3></A><P>Input (and output) file streams always have the <SAMP>in</SAMP> (or <SAMP>out</SAMP>) open mode flag set implicitly. An output file stream, for instance, knows that it is in output mode and you do not have to set the output mode explicitly. Instead of writing:</P><PRE>ofstream Str("out.txt",ios_base::out|ios_base::app);</PRE><P>you can simply say:</P><PRE>ofstream Str("out.txt",ios_base::app);</PRE><P>Bidirectional file streams, on the other hand, do not have the flag set implicitly. This is because a bidirectional stream does not have to be in both input and output mode in all cases. You might want to open a bidirectional stream for reading only or writing only. Bidirectional file streams therefore have no implicit input or output mode. You always have to set a bidirectional file stream's open mode explicitly.</P><A NAME="The Open modes <SAMP>ate</SAMP>, <SAMP>app</SAMP>, and <SAMP>trunc</SAMP>"><H3>The Open modes <SAMP>ate</SAMP>, <SAMP>app</SAMP>, and <SAMP>trunc</SAMP></H3></A><P>Each file maintains a <I>file position</I> that indicates the position in the file where the next byte will be read or written. When a file is opened, the initial file position is usually at the beginning of the file. The open modes <SAMP>ate</SAMP> (meaning <I>at end</I>) and <SAMP>app</SAMP> (meaning <I>append</I>) change this default to the end of the file.</P><P>There is a subtle difference between <SAMP>ate</SAMP> and <SAMP>app</SAMP> mode. If the file is opened in append mode, all output to the file is done at the current end of the file, regardless of intervening repositioning. Even if you modify the file position to a position before the file's end, you cannot write there. With at-end mode, you can navigate to a position before the end of file and write to it.</P><P>If you open an already existing file for writing, you usually want to overwrite the content of the output file. The open mode <SAMP>trunc</SAMP> (meaning <I>truncate</I>) has the effect of discarding the file content, in which case the initial file length is set to zero. Therefore, if you want to replace a file's content rather than extend the file, you have to open the file in <SAMP>out|trunc</SAMP>.<A HREF="endnote2.htm#fn27">[27]</A> Note that the file position will be at the beginning of the file in this case, which is exactly what you expect for overwriting an output file.</P><P>If you want to extend an output file, you open it in at-end or append mode. In this case, the file content is retained because the <SAMP>trunc</SAMP> flag is not set, and the initial file position is at the file's end. However, you may additionally set the <SAMP>trunc</SAMP> flag; the file content will be discarded and the output will be done at the end of an empty file.</P><P>Input mode only works for files that already exist. Otherwise, the stream construction will fail, as indicated by <SAMP>failbit</SAMP> set in the stream state. Files that are opened for writing will be created if they do not yet exist. The constructor only fails if the file cannot be created.</P><A NAME="The <SAMP>binary</SAMP> Open Mode"><H3>The <SAMP>binary</SAMP> Open Mode</H3></A><P>The <SAMP>binary</SAMP> open mode is explained in <A HREF="fil_2309.htm#2.5.4.3">section 2.5.4.3</A>.</P><A NAME="2.5.4.2"><H4>2.5.4.2 Combining Open Modes</H4></A><P>The effect of combining these open modes is similar to the mode argument of the C library function <SAMP>fopen(name,mode)</SAMP>. Table 8 gives an overview of all permitted combinations of open modes for text files and their counterparts in C stdio. Combinations of modes that are not listed in the table (such as both <SAMP>trunc</SAMP> and <SAMP>app</SAMP>) are invalid, and the attempted <SAMP>open()</SAMP> operation will fail.</P><H4>Table 8: Open modes and their C stdio counterparts</H4><CENTER><TABLE BORDER CELLSPACING=3 CELLPADDING=3><TR VALIGN=top><TD><B>Open Mode</B><BR></TD><TD><B>C stdio </B><BR><B>Equivalent</B><BR></TD><TD><B>Effect</B><BR></TD></TR><TR VALIGN=top><TD><SAMP>in</SAMP><BR></TD><TD><SAMP>"r"</SAMP><BR></TD><TD>Open text file for reading only<BR></TD></TR><TR VALIGN=top><TD><SAMP>out|trunc</SAMP><BR><SAMP>out</SAMP><BR></TD><TD><SAMP>"w"</SAMP><BR></TD><TD>Truncate to zero length, if existent, or create text file for writing only<BR></TD></TR><TR VALIGN=top><TD><SAMP>out|app</SAMP><BR></TD><TD><SAMP>"a"</SAMP><BR></TD><TD>Append; open or create text file only for writing at end of file<BR></TD></TR><TR VALIGN=top><TD><SAMP>in|out</SAMP><BR></TD><TD><SAMP>"r+"</SAMP><BR></TD><TD>Open text file for update (reading and writing)<BR></TD></TR><TR VALIGN=top><TD><SAMP>in|out|trunc</SAMP><BR></TD><TD><SAMP>"w+"</SAMP><BR></TD><TD>Truncate to zero length, if existent, or create text file for update<BR></TD></TR><TR VALIGN=top><TD><SAMP>in|out|app</SAMP><BR></TD><TD><SAMP>"a+"</SAMP><BR></TD><TD>Append; open or create text file for update, writing at end of file<BR></TD></TR></TABLE></CENTER><A NAME="2.5.4.3"><H4>2.5.4.3 Default Open Modes</H4></A><P>The open mode parameter in constructors and open() functions of file stream classes have a default value. The default open modes are listed in Table 9. Note that abbreviations are used; e.g., <SAMP>ifstream</SAMP> stands for <SAMP>basic_ifstream <charT,traits></SAMP>.</P><H4>Table 9: Default open modes</H4><CENTER><TABLE BORDER CELLSPACING=3 CELLPADDING=3><TR VALIGN=top><TD><B>File Stream</B><BR></TD><TD><B>Default Open Mode</B><BR></TD></TR><TR VALIGN=top><TD><SAMP>ifstream</SAMP><BR></TD><TD><SAMP>in</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>ofstream</SAMP><BR></TD><TD><SAMP>out</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>fstream</SAMP><BR></TD><TD><SAMP>in|out</SAMP><BR></TD></TR></TABLE></CENTER><A NAME="2.5.5"><H3>2.5.5 Binary and Text Mode</H3></A><P>The representation of text files varies among operating systems. For example, the end of a line in a UNIX environment is represented by the <I>linefeed</I> character <SAMP>'\n'</SAMP>. On PC-based systems, the end of the line consists of two characters, <I>carriage</I> <I>return</I> <SAMP>'\r'</SAMP> and <I>linefeed</I> <SAMP>'\n'</SAMP>. The end of the file differs as well on these two operating systems. Peculiarities on other operating systems are also conceivable.</P><P>To make programs more portable among operating systems, an automatic conversion can be done on input and output. The carriage return or linefeed sequence, for example, can be converted to a single <SAMP>'\n'</SAMP> character on input; the <SAMP>'\n'</SAMP> can be expanded to <SAMP>"\r\n"</SAMP> on output. This conversion mode is called <I>text mode</I>, as opposed to<I> binary mode</I>. In binary mode, no such conversions are performed.</P><P>The mode flag <SAMP>ios_base::binary</SAMP> has the effect of opening a file in binary mode. This has the effect described above; in other words, all automatic conversions, such as converting <SAMP>"\r\n"</SAMP> to <SAMP>'\n',</SAMP> will be suppressed. <A HREF="endnote2.htm#fn28">[28]</A></P><P>If you have to process a binary file you should always set the <SAMP>binary</SAMP> mode flag, because most likely you will not want any kind of implicit, system-specific conversion being performed.</P><P>The effect of the binary open mode is frequently misunderstood. It does <I>not </I>put the inserters and extractors into a binary mode, and hence suppress the formatting they usually perform. Binary input and output is done solely by <SAMP>basic_istream <charT> ::read()</SAMP> and <SAMP>basic_ostream <charT> ::write()</SAMP>.</P><HR><A HREF="err_7848.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc2.htm"><IMG SRC="images/toc.gif"></A><A HREF="inm_4073.htm"><IMG SRC="images/next.gif"></A><P>©Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -