⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut6-1.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<DD>It is the most generic: returns <TT><B>false</B></TT> in the same cases in which 
calling any of the previous functions would return <TT><B>true</B></TT>.
</DL>

In order to reset the state flags checked by the previous member functions
you can use member function <TT><B>clear()</B></TT>, with no parameters.

<P>
<H2><I>get</I> and <I>put</I> stream pointers</H2>
All i/o streams objects have, at least, one stream pointer:<BR>
<UL>
<LI><TT><B>ifstream</B></TT>, like <TT><B>istream</B></TT>, has a pointer known as <I>get pointer</I> that points to the next element to be read.<BR>
<LI><TT><B>ofstream</B></TT>, like <TT><B>ostream</B></TT>, has a pointer <I>put pointer</I> that points to the location where the next element has to be written.<BR>
<LI>Finally <TT><B>fstream</B></TT>, like <TT><B>iostream</B></TT>, inherits both: <I>get</I> and <I>put</I>
</UL>
<P>
These stream pointers that point to the reading or writing locations within a stream
can be read and/or manipulated using the following member functions:
<DL>
<DT><TT><B>tellg()</B></TT> and <TT><B>tellp()</B></TT>
<DD>
These two member functions admit no parameters and return a value of type
<TT><B>pos_type</B></TT> (according ANSI-C++ standard) that is an integer data type
representing the current position of <I>get</I> stream pointer (in case of <TT>tellg</TT>)
or <I>put</I> stream pointer (in case of <TT>tellp</TT>).
<P>
<DT><TT><B>seekg()</B></TT> and <TT><B>seekp()</B></TT>
<DD>This pair of functions serve respectively to change the position of stream pointers
<I>get</I> and <I>put</I>. Both functions are overloaded with two different prototypes: 
<DL>
 <P><DT><TT>
  <B>seekg ( pos_type </B><I>position</I><B> );</B><BR>
  <B>seekp ( pos_type </B><I>position</I><B> );</B><BR>
  </TT>
 <DD>
Using this prototype the stream pointer is changed to an absolute position from the
beginning of the file. The type required is the same as that returned by
functions <TT><B>tellg</B></TT> and <TT><B>tellp</B></TT>. 

<P><DT><TT>
  <B>seekg ( off_type </B><I>offset</I><B>, seekdir </B><I>direction</I><B> );</B><BR>
  <B>seekp ( off_type </B><I>offset</I><B>, seekdir </B><I>direction</I><B> );</B><BR>
  </TT>
 <DD>
Using this prototype, an offset from a concrete point
determined by parameter <I>direction</I> can be specified. It can be:
<BLOCKQUOTE><TABLE BORDER=1>
<TR><TD><TT><B>ios::beg</B></TT></TD><TD>offset specified from the beginning of the stream</TD></TR>
<TR><TD><TT><B>ios::cur</B></TT></TD><TD>offset specified from the current position of the stream pointer</TD></TR>
<TR><TD><TT><B>ios::end</B></TT></TD><TD>offset specified from the end of the stream</TD></TR>
</TABLE></BLOCKQUOTE>

 </DL>
</DL>
The values of both stream pointers <I>get</I> and <I>put</I>
are counted in different ways for text files than for binary files, since in
text mode files some modifications to the appearance of some special characters
can occur. For that reason it is advisable to
use only the first prototype of <TT><B>seekg</B></TT> and <TT><B>seekp</B></TT>
with files opened in text mode
and always use non-modified values returned by
<TT><B>tellg</B></TT> or <TT><B>tellp</B></TT>.
With binary files, you can freely use all the implementations for these functions.
They should not have any unexpected behavior.

<P>
The following example uses the member functions just seen to obtain the size of a binary
file:

<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// obtaining file size</I>
#include &lt;iostream.h&gt;
#include &lt;fstream.h&gt;

const char * filename = "example.txt";

int main () {
  long l,m;
  ifstream file (filename, ios::in|ios::binary);
  l = file.tellg();
  file.seekg (0, ios::end);
  m = file.tellg();
  file.close();
  cout &lt;&lt; "size of " &lt;&lt; filename;
  cout &lt;&lt; " is " &lt;&lt; (m-l) &lt;&lt; " bytes.\n";
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top"><TT>
<B>size of example.txt is 40 bytes.</B>
</TT></TD></TR></TABLE>
</CENTER>

<P>
<H2>Binary files</H2>
In binary files inputting and outputting data with operators like 
<TT><B>&lt;&lt;</B></TT> and <TT><B>&gt;&gt;</B></TT> and functions like
<TT><B>getline</B></TT>, does not make too much sense, although they are
perfectly valid.
<P>
File streams include two member functions specially designed for input
and output of data sequentially: <B>write</B> and <B>read</B>. 
The first one (<B>write</B>) is a member function of <TT><B>ostream</B></TT>,
also inherited by <TT><B>ofstream</B></TT>. And <B>read</B> is member function of
<TT><B>istream</B></TT> and it is inherited by <TT><B>ifstream</B></TT>.
Objects of class <TT><B>fstream</B></TT> have both. Their prototypes are:
<BLOCKQUOTE><TT>
<B>write ( char * </B><I>buffer</I><B>, streamsize </B><I>size</I><B> );</B><BR>
<B>&nbsp;read ( char * </B><I>buffer</I><B>, streamsize </B><I>size</I><B> );</B><BR>
</TT></BLOCKQUOTE>
Where <TT><I>buffer</I></TT> is the address of a memory block where the read data are stored
or from where the data to be written are taken.
The <TT><I>size</I></TT> parameter is an integer value that specifies the number of
characters to be read/written from/to <I>the buffer</I>.
</DL>

<P>
<CENTER>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// reading binary file</I>
#include &lt;iostream.h&gt;
#include &lt;fstream.h&gt;

const char * filename = "example.txt";

int main () {
  char * buffer;
  long size;
  ifstream file (filename, ios::in|ios::binary|ios::ate);
  size = file.tellg();
  file.seekg (0, ios::beg);
  buffer = new char [size];
  file.read (buffer, size);
  file.close();

  cout &lt;&lt; "the complete file is in a buffer";

  delete[] buffer;
  return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="silver" WIDTH=50% VALIGN="top"><TT>
<B>the complete file is in a buffer</B>
</TT></TD></TR></TABLE>
</CENTER>

<P>
<H2>Buffers and Synchronization</H2>
When we operate with file streams, these are associated to a <I>buffer</I>
of type <TT><B>streambuf</B></TT>.  This <I>buffer</I> is a memory block that
acts as an intermediary between the stream and the physical file.
For example, with an out stream, each time the member function
<TT><B>put</B></TT> (write a single character) is called, the character
is not written directly to the physical file with which the stream is associated.  
Instead of that, the character is inserted in the
<I>buffer</I> for that stream.
<P>
When the buffer is flushed, all data that it contains is written to the physic media
(if it is an out stream) or simply erased (if it is an in stream).
This process is called synchronization and it takes place under any of the following
circumstances:
<UL>
<LI><B>When the file is closed:</B> before closing a file all buffers that have not yet been completely written or read are synchronized.
<LI><B>When the <I>buffer</I> is full:</B>  <I>Buffers</I> have a certain size.  When the <I>buffer</I> is full it is automatically synchronized.
<LI><B>Explicitly with manipulators:</B> When certain manipulators are used on streams a synchronization takes place.  These manipulators are: <TT><B>flush</B></TT> and <TT><B>endl</B></TT>.
<LI><B>Explicitly with function <TT>sync()</TT>:</B> Calling member function <TT><B>sync()</B></TT> (no parameters) causes an immediate syncronization.  This function returns an <TT><B>int</B></TT> value equal to <TT><B>-1</B></TT> if the stream has no associated <I>buffer</I> or in case of failure.
</UL>

<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
 <TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
 <TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>&copy; The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>

<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=50%><A HREF="tut5-5.html">
 <IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
 Previous:<BR><B>5-5. Preprocessor directives.</B></A></TD>
<TD ALIGN="left" WIDTH=50%><A HREF="index.html">
 <IMG SRC="butnindx.gif" BORDER=0><BR>
 index</A></TD>
</TR></TABLE>
</CENTER>

<!--/cuatut-->

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -