A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FFile_Stream%2Fostream</id>
		<title>C++ Tutorial/File Stream/ostream - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FFile_Stream%2Fostream"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/ostream&amp;action=history"/>
		<updated>2026-04-11T06:39:50Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/ostream&amp;diff=2934&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/ostream&amp;diff=2934&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/ostream&amp;diff=2935&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/ostream&amp;diff=2935&amp;oldid=prev"/>
				<updated>2010-05-25T10:31:08Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Define your own ostream variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include&amp;lt;iostream.h&amp;gt;&lt;br /&gt;
ostream &amp;amp; setthex(ostream &amp;amp; stream)&lt;br /&gt;
{&lt;br /&gt;
  stream.setf(ios::hex|ios::uppercase|ios::showbase);&lt;br /&gt;
  return stream;&lt;br /&gt;
}&lt;br /&gt;
ostream &amp;amp; reset(ostream &amp;amp; stream)&lt;br /&gt;
{&lt;br /&gt;
  stream.unsetf(ios::hex|ios::uppercase|ios::showbase);&lt;br /&gt;
  return stream;&lt;br /&gt;
}&lt;br /&gt;
main()&lt;br /&gt;
{&lt;br /&gt;
  cout&amp;lt;&amp;lt;setthex&amp;lt;&amp;lt;200&amp;lt;&amp;lt;&amp;quot;\n&amp;quot;;&lt;br /&gt;
  cout&amp;lt;&amp;lt;reset&amp;lt;&amp;lt;200&amp;lt;&amp;lt;&amp;quot;\n&amp;quot;;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;200&lt;br /&gt;
200&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==file descriptor out buffer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;streambuf&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;
// for write():&lt;br /&gt;
#ifdef _MSC_VER&lt;br /&gt;
# include &amp;lt;io.h&amp;gt;&lt;br /&gt;
#else&lt;br /&gt;
# include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#endif&lt;br /&gt;
class fdoutbuf : public std::streambuf {&lt;br /&gt;
  protected:&lt;br /&gt;
    int fd;    // file descriptor&lt;br /&gt;
  public:&lt;br /&gt;
    // constructor&lt;br /&gt;
    fdoutbuf (int _fd) : fd(_fd) {&lt;br /&gt;
    }&lt;br /&gt;
  protected:&lt;br /&gt;
    // write one character&lt;br /&gt;
    virtual int_type overflow (int_type c) {&lt;br /&gt;
        if (c != EOF) {&lt;br /&gt;
            char z = c;&lt;br /&gt;
            if (write (fd, &amp;amp;z, 1) != 1) {&lt;br /&gt;
                return EOF;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return c;&lt;br /&gt;
    }&lt;br /&gt;
    // write multiple characters&lt;br /&gt;
    virtual&lt;br /&gt;
    std::streamsize xsputn (const char* s,&lt;br /&gt;
                            std::streamsize num) {&lt;br /&gt;
        return write(fd,s,num);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
class fdostream : public std::ostream {&lt;br /&gt;
  protected:&lt;br /&gt;
    fdoutbuf buf;&lt;br /&gt;
  public:&lt;br /&gt;
    fdostream (int fd) : std::ostream(0), buf(fd) {&lt;br /&gt;
        rdbuf(&amp;amp;buf);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    fdostream out(1);    // stream with buffer writing to file descriptor 1&lt;br /&gt;
    out &amp;lt;&amp;lt; &amp;quot;31 hexadecimal: &amp;quot; &amp;lt;&amp;lt; std::hex &amp;lt;&amp;lt; 31 &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;31 hexadecimal: 1f&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Hex out stream==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
void hexMultiplicationTable (std::streambuf* buffer, int num)&lt;br /&gt;
{&lt;br /&gt;
    std::ostream hexout(buffer);&lt;br /&gt;
    hexout &amp;lt;&amp;lt; std::hex &amp;lt;&amp;lt; std::showbase;&lt;br /&gt;
    for (int i=1; i&amp;lt;=num; ++i) {&lt;br /&gt;
        for (int j=1; j&amp;lt;=10; ++j) {&lt;br /&gt;
            hexout &amp;lt;&amp;lt; i*j &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
        hexout &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
    }&lt;br /&gt;
}   // does NOT close buffer&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    using namespace std;&lt;br /&gt;
    int num = 5;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;We print &amp;quot; &amp;lt;&amp;lt; num&lt;br /&gt;
         &amp;lt;&amp;lt; &amp;quot; lines hexadecimal&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    hexMultiplicationTable(cout.rdbuf(),num);&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;That was the output of &amp;quot; &amp;lt;&amp;lt; num&lt;br /&gt;
         &amp;lt;&amp;lt; &amp;quot; hexadecimal lines &amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;We print 5 lines hexadecimal&lt;br /&gt;
0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa&lt;br /&gt;
0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10 0x12 0x14&lt;br /&gt;
0x3 0x6 0x9 0xc 0xf 0x12 0x15 0x18 0x1b 0x1e&lt;br /&gt;
0x4 0x8 0xc 0x10 0x14 0x18 0x1c 0x20 0x24 0x28&lt;br /&gt;
0x5 0xa 0xf 0x14 0x19 0x1e 0x23 0x28 0x2d 0x32&lt;br /&gt;
That was the output of 5 hexadecimal lines&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Open file &amp;quot;&amp;quot;example.dat&amp;quot;&amp;quot; for reading and writing==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    // open file &amp;quot;&amp;quot;example.dat&amp;quot;&amp;quot; for reading and writing&lt;br /&gt;
    filebuf buffer;&lt;br /&gt;
    ostream output(&amp;amp;buffer);&lt;br /&gt;
    istream input(&amp;amp;buffer);&lt;br /&gt;
    buffer.open (&amp;quot;example.dat&amp;quot;, ios::in | ios::out | ios::trunc);&lt;br /&gt;
    for (int i=1; i&amp;lt;=4; i++) {&lt;br /&gt;
        // write one line&lt;br /&gt;
        output &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;. line&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
        // print all file contents&lt;br /&gt;
        input.seekg(0);          // seek to the beginning&lt;br /&gt;
        char c;&lt;br /&gt;
        while (input.get(c)) {&lt;br /&gt;
            cout.put(c);&lt;br /&gt;
        }&lt;br /&gt;
        cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
        input.clear();           // clear  eofbit and  failbit&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1. line&lt;br /&gt;
1. line&lt;br /&gt;
2. line&lt;br /&gt;
1. line&lt;br /&gt;
2. line&lt;br /&gt;
3. line&lt;br /&gt;
1. line&lt;br /&gt;
2. line&lt;br /&gt;
3. line&lt;br /&gt;
4. line&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Redirect ouput into the file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
void redirect(ostream&amp;amp;);&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the first row&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    redirect(cout);&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the last row&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
void redirect (ostream&amp;amp; strm)&lt;br /&gt;
{&lt;br /&gt;
    ofstream file(&amp;quot;redirect.txt&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
    // save output buffer of the stream&lt;br /&gt;
    streambuf* strm_buffer = strm.rdbuf();&lt;br /&gt;
    // redirect ouput into the file&lt;br /&gt;
    strm.rdbuf (file.rdbuf());&lt;br /&gt;
    file &amp;lt;&amp;lt; &amp;quot;one row for the file&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    strm &amp;lt;&amp;lt; &amp;quot;one row for the stream&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    // restore old output buffer&lt;br /&gt;
    strm.rdbuf (strm_buffer);&lt;br /&gt;
}    // closes file AND its buffer automatically&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;the first row&lt;br /&gt;
the last row&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==stream for hexadecimal standard output==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    // stream for hexadecimal standard output&lt;br /&gt;
    ostream hexout(cout.rdbuf());&lt;br /&gt;
    hexout.setf (ios::hex, ios::basefield);&lt;br /&gt;
    hexout.setf (ios::showbase);&lt;br /&gt;
    // switch between decimal and hexadecimal output&lt;br /&gt;
    hexout &amp;lt;&amp;lt; &amp;quot;hexout: &amp;quot; &amp;lt;&amp;lt; 177 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    cout   &amp;lt;&amp;lt; &amp;quot;cout: &amp;quot;   &amp;lt;&amp;lt; 177 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    hexout &amp;lt;&amp;lt; &amp;quot;hexout: &amp;quot; &amp;lt;&amp;lt; -49 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    cout   &amp;lt;&amp;lt; &amp;quot;cout: &amp;quot;   &amp;lt;&amp;lt; -49 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    hexout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;hexout: 0xb1 cout: 177 hexout: 0xffffffcf cout: -49&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Switch between decimal and hexadecimal output==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    // stream for hexadecimal standard output&lt;br /&gt;
    ostream hexout(cout.rdbuf());&lt;br /&gt;
    hexout.setf (ios::hex, ios::basefield);&lt;br /&gt;
    hexout.setf (ios::showbase);&lt;br /&gt;
    // switch between decimal and hexadecimal output&lt;br /&gt;
    hexout &amp;lt;&amp;lt; &amp;quot;hexout: &amp;quot; &amp;lt;&amp;lt; 177 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    cout   &amp;lt;&amp;lt; &amp;quot;cout: &amp;quot;   &amp;lt;&amp;lt; 177 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    hexout &amp;lt;&amp;lt; &amp;quot;hexout: &amp;quot; &amp;lt;&amp;lt; -49 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    cout   &amp;lt;&amp;lt; &amp;quot;cout: &amp;quot;   &amp;lt;&amp;lt; -49 &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    hexout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;hexout: 0xb1 cout: 177 hexout: 0xffffffcf cout: -49&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>