use_sbuf.cpp
上传用户:dq031136
上传日期:2022-08-08
资源大小:802k
文件大小:1k
- // 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";
- }
- }