Connect ofstream and streambuf and operations with streambufs.
#include <iostream>
#include <fstream>
using namespace std;
int main(void){
int c;
char *filename = "test.txt";
ofstream outfile;
streambuf *out, *input = cin.rdbuf();
outfile.open( filename, ios::ate | ios::app);
if (!outfile)
{
cerr << "Could not open " << filename;
return(-1);
}
out = outfile.rdbuf();
while ( (c = input -> sbumpc() ) != EOF){
cout << char(c); // Echo to screen.
if (out -> sputc(c) == EOF)
cerr << "Output error";
}
}
Related examples in the same category