| 1 |
#include "oformstream.h" |
| 2 |
#include <memory> |
| 3 |
#include <stdarg.h> |
| 4 |
|
| 5 |
std::ostream &oformstream::form(char const *fmt, ...) // 1 |
| 6 |
{ |
| 7 |
va_list list; |
| 8 |
va_start(list, fmt); |
| 9 |
|
| 10 |
size_t n = vsnprintf(0, 0, fmt, list); // 2 |
| 11 |
std::auto_ptr<char> buf(new char[n + 1]); // 3 |
| 12 |
vsprintf(buf.get(), fmt, list); // 4 |
| 13 |
|
| 14 |
return *this << buf.get(); // 5 |
| 15 |
} |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
|
| 19 |
int main() |
| 20 |
{ |
| 21 |
oformstream |
| 22 |
of(cout); |
| 23 |
|
| 24 |
of << "Hello world\n"; |
| 25 |
cout << "Ok, "; |
| 26 |
of << "That's all, folks\n"; |
| 27 |
|
| 28 |
of.form("%s\n", "Hello world of C++") << '\n'; |
| 29 |
}; |
| 30 |
|
| 31 |
/* |
| 32 |
Generated output: |
| 33 |
|
| 34 |
Hello world |
| 35 |
Ok, That's all, folks |
| 36 |
Hello world of C++ |
| 37 |
*/ |