PRODUCT : Borland C++ NUMBER : 1550 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/2 TITLE : Using Buffers with I/O String Streams // Using Buffers with I/O String Streams #include #include #include int main() { // note trailing space, since we will parse by spaces. char szInbuf0[] = "Good Morning Vietnam "; // must pass _exact_ size of szInbuf0 into istrstream // ctor; strstream won't set eof until it has read that // number of bytes istrstream ins0(szInbuf0, strlen(szInbuf0)); cout << "Reading in string mode:" << endl; char string1[20]; ins0.getline(string1,20,' '); while (!ins0.eof()) { cout << string1 << endl; ins0.getline(string1,20,' '); } cout << "Reading in char mode:" << endl; strcpy(szInbuf0, "Good Night Gracie"); istrstream ins1(szInbuf0, strlen(szInbuf0)); // note: if you need to know how much is left in the // buffer, use ins1.rdbuf()->in_avail() or ins1.rdbuf()->out_avail(); char c; ins1.get(c); while(!ins1.eof()) { cout << c << '.'; ins1.get(c); } cout << endl; // instead of constructing a new strstream, we // might think of just changing the buffer ... but // strstreambuf::setbuf() can't change your buffer PRODUCT : Borland C++ NUMBER : 1550 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/2 TITLE : Using Buffers with I/O String Streams // (it can only expand the buffer size.) // a dynamic i/o strstream; will be automatically // upsized as needed to accomodate more data char string2[20], string3[20]; strstream s3; s3 << "New World Order!"; s3 >> string1 >> string2 >> string3; cout << "Dynamic strstream: " << string1 << '.' << string2 << '.' << string3 << endl; // putting a new string in // return both put & get pointers to beginning of buffer s3.seekp( 0, ios::beg ); s3.seekg( 0, ios::beg ); // clear eof bit s3.clear(); // note: strstream buffer will _not_ be shrunk, so now // we have New Pizza Order! in the buffer s3 << "New Pizza"; s3 >> string1 >> string2 >> string3; cout << "Dynamic strstream after reset: " << string1 << '.' << string2 << '.' << string3 << endl; return 0; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.