Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
strstreambuf.cpp
Go to the documentation of this file.
1
29#include <iomanip> // for setw
30#include <ios> // for dec, ends
31#include <iostream> // for cerr, cout
32#include <strstream> // for istream, ostrstream
33
34#include <examples.h>
35
36
37int main ()
38{
39 // create a write-only stream object
40 std::ostrstream out;
41
42 // tie the istream object to the ostrstream object
43 std::istream in (out.rdbuf ());
44
45 // output to out
46 out << "Anticonstitutionnellement is a big word!!!\n";
47
48 // create a NTBS
49 const char s[] ="Le rat des villes et le rat des champs.";
50
51 // output the NTBS
52 out << s << '\n';
53
54 // (try to) resize the buffer
55 if (out.rdbuf ()->pubsetbuf (0, 128L))
56 std::cout << "Successfully allocated buffer.\n";
57 else
58 std::cerr << "Failed to allocate buffer.\n";
59
60 // output the contents of the buffer to standard output
61 std::cout << in.rdbuf ();
62
63 // format a number into out
64 out << std::dec // decimal base
65 << std::setfill ('#') // set fill character
66 << std::setw (16) // set field width
67 << 0x100 << '\n'; // format
68
69 // output the content of the input sequence to standard output
70 std::cout << in.rdbuf( ) << '\n';
71
72 // number of elements in the output sequence
73 const std::streamsize pcount = std::streamsize (out.rdbuf ()->pcount ());
74
75 std::cout << "Buffer size is " << pcount << '\n';
76
77 // (try to) resize the buffer
78 if (out.rdbuf ()->pubsetbuf (0, pcount * 2L))
79 std::cout << "\nSuccessfully resized buffer to " << pcount * 2 << '\n';
80 else
81 std::cerr << "\nFailed to resize buffer to " << pcount * 2 << '\n';
82
83 // NUL-terminate the character array before streaming it out
84 out << std::ends;
85
86 // output the contents of the streambuf object associated with out
87 std::cout << out.rdbuf ()->str ();
88
89 // unfreeze the streambuf so it cleans up its allocated memory
90 out.rdbuf ()->freeze (false);
91
92 return 0;
93}
#define s
const int out
const int in
int main()