C++ Tutorial/File Stream/ostream

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Define your own ostream variable

<source lang="cpp">#include<iostream.h> ostream & setthex(ostream & stream) {

 stream.setf(ios::hex|ios::uppercase|ios::showbase);
 return stream;

} ostream & reset(ostream & stream) {

 stream.unsetf(ios::hex|ios::uppercase|ios::showbase);
 return stream;

} main() {

 cout<<setthex<<200<<"\n";
 cout<<reset<<200<<"\n";
 return 0;

}</source>

200
200

file descriptor out buffer

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <streambuf>
  3. include <cstdio>

// for write():

  1. ifdef _MSC_VER
  2. include <io.h>
  3. else
  4. include <unistd.h>
  5. endif

class fdoutbuf : public std::streambuf {

 protected:
   int fd;    // file descriptor
 public:
   // constructor
   fdoutbuf (int _fd) : fd(_fd) {
   }
 protected:
   // write one character
   virtual int_type overflow (int_type c) {
       if (c != EOF) {
           char z = c;
           if (write (fd, &z, 1) != 1) {
               return EOF;
           }
       }
       return c;
   }
   // write multiple characters
   virtual
   std::streamsize xsputn (const char* s,
                           std::streamsize num) {
       return write(fd,s,num);
   }

}; class fdostream : public std::ostream {

 protected:
   fdoutbuf buf;
 public:
   fdostream (int fd) : std::ostream(0), buf(fd) {
       rdbuf(&buf);
   }

};

int main() {

   fdostream out(1);    // stream with buffer writing to file descriptor 1
   out << "31 hexadecimal: " << std::hex << 31 << std::endl;

}</source>

31 hexadecimal: 1f

Hex out stream

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <fstream>

void hexMultiplicationTable (std::streambuf* buffer, int num) {

   std::ostream hexout(buffer);
   hexout << std::hex << std::showbase;
   for (int i=1; i<=num; ++i) {
       for (int j=1; j<=10; ++j) {
           hexout << i*j << " ";
       }
       hexout << std::endl;
   }

} // does NOT close buffer int main() {

   using namespace std;
   int num = 5;
   cout << "We print " << num
        << " lines hexadecimal" << endl;
   hexMultiplicationTable(cout.rdbuf(),num);
   cout << "That was the output of " << num
        << " hexadecimal lines " << endl;

}</source>

We print 5 lines hexadecimal
0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa
0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10 0x12 0x14
0x3 0x6 0x9 0xc 0xf 0x12 0x15 0x18 0x1b 0x1e
0x4 0x8 0xc 0x10 0x14 0x18 0x1c 0x20 0x24 0x28
0x5 0xa 0xf 0x14 0x19 0x1e 0x23 0x28 0x2d 0x32
That was the output of 5 hexadecimal lines

Open file ""example.dat"" for reading and writing

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <fstream>

using namespace std; int main() {

   // open file ""example.dat"" for reading and writing
   filebuf buffer;
   ostream output(&buffer);
   istream input(&buffer);
   buffer.open ("example.dat", ios::in | ios::out | ios::trunc);
   for (int i=1; i<=4; i++) {
       // write one line
       output << i << ". line" << endl;
       // print all file contents
       input.seekg(0);          // seek to the beginning
       char c;
       while (input.get(c)) {
           cout.put(c);
       }
       cout << endl;
       input.clear();           // clear  eofbit and  failbit
   }

}</source>

1. line
1. line
2. line
1. line
2. line
3. line
1. line
2. line
3. line
4. line

Redirect ouput into the file

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <fstream>

using namespace std; void redirect(ostream&); int main() {

   cout << "the first row" << endl;
   redirect(cout);
   cout << "the last row" << endl;

} void redirect (ostream& strm) {

   ofstream file("redirect.txt");
   
   // save output buffer of the stream
   streambuf* strm_buffer = strm.rdbuf();
   // redirect ouput into the file
   strm.rdbuf (file.rdbuf());
   file << "one row for the file" << endl;
   strm << "one row for the stream" << endl;
   // restore old output buffer
   strm.rdbuf (strm_buffer);

} // closes file AND its buffer automatically</source>

the first row
the last row

stream for hexadecimal standard output

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <fstream>

using namespace std; int main() {

   // stream for hexadecimal standard output
   ostream hexout(cout.rdbuf());
   hexout.setf (ios::hex, ios::basefield);
   hexout.setf (ios::showbase);
   // switch between decimal and hexadecimal output
   hexout << "hexout: " << 177 << " ";
   cout   << "cout: "   << 177 << " ";
   hexout << "hexout: " << -49 << " ";
   cout   << "cout: "   << -49 << " ";
   hexout << endl;

}</source>

hexout: 0xb1 cout: 177 hexout: 0xffffffcf cout: -49

Switch between decimal and hexadecimal output

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <fstream>

using namespace std; int main() {

   // stream for hexadecimal standard output
   ostream hexout(cout.rdbuf());
   hexout.setf (ios::hex, ios::basefield);
   hexout.setf (ios::showbase);
   // switch between decimal and hexadecimal output
   hexout << "hexout: " << 177 << " ";
   cout   << "cout: "   << 177 << " ";
   hexout << "hexout: " << -49 << " ";
   cout   << "cout: "   << -49 << " ";
   hexout << endl;

}</source>

hexout: 0xb1 cout: 177 hexout: 0xffffffcf cout: -49