use_sbuf.cpp

来自「《C/C++程序员实用大全》配套代码,适用初学C++的人员。」· C++ 代码 · 共 30 行

CPP
30
字号
// Operations with streambufs.
#include <iostream.h>
#include <fstream.h>

void main(void)
 {
   int c;
   const char *filename = "_junk_.$$$";
   ofstream outfile;
   streambuf *out, *input = cin.rdbuf();

   // Position at the end of file. Append all text.
   outfile.open( filename, ios::ate | ios::app);
   if (!outfile) 
    {
      cerr << "Could not open " << filename;
      return(-1);
    }

   out = outfile.rdbuf();  // Connect ofstream and streambuf.

   clog << "Input some text. Use Control-Z to end." << endl;
   while ( (c = input -> sbumpc() ) != EOF)
    {
      cout << char(c);                         // Echo to screen.
      if (out -> sputc(c) == EOF)
         cerr << "Output error";
      }
 }

⌨️ 快捷键说明

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