/////////////////////////////////////////////////////////////// // filebuf.cpp // // demonstrate low level input and output using filebufs // // built from FILE pointer and stdout // // // // Jim Fawcett, 24 Mar 96, modified 23 Mar 97 // /////////////////////////////////////////////////////////////// #include // cout, << #include // ifstream(), <<, >> // note: will not compile with the modern version given below // I believe that is a defect with VC 6.0 fstream // // #include // #include // using namespace std; #include // exit(1); #include // fopen(), FILE #include //----< display titles >--------------------------------------- void title(const char *t, char ul = '-', ostream &out = cout) { int len = strlen(t) + 2; std::string line(len,ul); if(ul == '=') out << "\n " << line.c_str(); out << "\n " << t << "\n " << line.c_str() << endl; } //----< begin demonstration >---------------------------------- void main(int argc, char *argv[]) { ////////////////////////////////////////////////////////////// title("Demonstrating use of Custom Buffers with File IO",'='); ////////////////////////////////////////////////////////////// if(argc < 2) { cout << "\nplease enter name of text file on command line\n"; exit(1); } // title("using custom buffer for input from file"); ///////////////////////////////////////////////// FILE *fptr = fopen(argv[argc-1],"r"); if(!fptr) { cout << "can't open file " << argv[argc-1] << endl; exit(1); } // build an input filebuf using custom input buffer and // file opened with stdio::fopen() const int bufSize = 256; char inbuf[bufSize]; // make custom buffer int handle = fileno(fptr); // get file handle for filebuf filebuf ifb(handle,inbuf,bufSize); // construct filebuf cout << &ifb << endl; // stream to cout cout.flush(); // build an output filebuf using custom output buffer and // stdio::stdout then attach to stream and fill from // input buffer defined above title("using custom buffer for stdout"); //////////////////////////////////////// char outbuf[bufSize]; // custom buffer filebuf ofb(1,outbuf,bufSize); // attach to stdout (handle=1) ostream out(&ofb); // now attach buffer to stream ifb.seekpos(0, ios::in); // move to top of input buffer out << &ifb; // stream it to the output // could replace last statement with the lower level // gets and puts shown below // char ch; // while((ch = ifb.sbumpc()) != EOF) // ofb.sputc(ch); cout << "\n\n"; }