use_sbuf.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. // Operations with streambufs.
  2. #include <iostream.h>
  3. #include <fstream.h>
  4. void main(void)
  5.  {
  6.    int c;
  7.    const char *filename = "_junk_.$$$";
  8.    ofstream outfile;
  9.    streambuf *out, *input = cin.rdbuf();
  10.    // Position at the end of file. Append all text.
  11.    outfile.open( filename, ios::ate | ios::app);
  12.    if (!outfile) 
  13.     {
  14.       cerr << "Could not open " << filename;
  15.       return(-1);
  16.     }
  17.    out = outfile.rdbuf();  // Connect ofstream and streambuf.
  18.    clog << "Input some text. Use Control-Z to end." << endl;
  19.    while ( (c = input -> sbumpc() ) != EOF)
  20.     {
  21.       cout << char(c);                         // Echo to screen.
  22.       if (out -> sputc(c) == EOF)
  23.          cerr << "Output error";
  24.       }
  25.  }