C++ Tutorial/File Stream/ostrstream — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:31, 25 мая 2010
Содержание
- 1 Check how many characters are stored in ostrstream
- 2 Display how many chars in ostrstream
- 3 Freeze dynamic buffer and return pointer to it
- 4 Make ostringstream work with decimal, hexadecimal value, floating value, bitset
- 5 ostrstream with format
- 6 Output of several data types to an ostringstream object: double, int and address of int
- 7 Save string with ostringstream
Check how many characters are stored in ostrstream
<source lang="cpp">#include <strstream>
- include <iostream>
using namespace std; main() {
char str[80]; ostrstream outs(str, sizeof(str)); outs << "Hello "; outs << 34 << " " << 1234.23; outs << ends; cout << outs.pcount(); // display how many chars in outs cout << " " << str; return 0;
}</source>
Display how many chars in ostrstream
<source lang="cpp">#include <strstream>
- include <iostream>
using namespace std; int main() {
char str[80]; ostrstream outs(str, sizeof(str)); outs << "abcdefg "; outs << 27 << " " << 890.23; outs << ends; // null terminate cout << outs.pcount(); // display how many chars in outs cout << " " << str; return 0;
}</source>
18 abcdefg 27 890.23"
Freeze dynamic buffer and return pointer to it
<source lang="cpp">#include <strstream>
- include <iostream>
using namespace std; int main() {
char *p; ostrstream outs; outs << "AAAAAAAAAAAAAAAAAAAA"; outs << -10 << hex << " "; outs.setf(ios::showbase); outs << 100 << ends; p = outs.str(); cout << p; return 0;
}</source>
AAAAAAAAAAAAAAAAAAAA-10 0x64"
Make ostringstream work with decimal, hexadecimal value, floating value, bitset
<source lang="cpp">/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <sstream>
- include <bitset>
using namespace std; int main() {
ostringstream os; // decimal and hexadecimal value os << "dec: " << 15 << hex << " hex: " << 15 << endl; cout << os.str() << endl; // append floating value and bitset bitset<15> b(5789); os << "float: " << 4.67 << " bitset: " << b << endl; // overwrite with octal value os.seekp(0); os << "oct: " << oct << 15; cout << os.str() << endl;
}</source>
dec: 15 hex: f oct: 17 hex: f float: 4.67 bitset: 001011010011101
ostrstream with format
<source lang="cpp">#include <strstream>
- include <iostream>
using namespace std; int main() {
char str[80]; ostrstream outs(str, sizeof(str)); outs << "AAAAAAAAAAAAAAAA. "; outs << 1024 << hex << " "; outs.setf(ios::showbase); outs << 100 << " " << 99.789 << ends; cout << str; // display string on console return 0;
}</source>
AAAAAAAAAAAAAAAA. 1024 0x64 99.789"
Output of several data types to an ostringstream object: double, int and address of int
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <string>
using std::string;
- include <sstream>
using std::ostringstream; int main() {
ostringstream outputString; string string1( "string " ); double double1 = 123.4567; int integer = 22; outputString << string1 << double1 << integer << &integer; // call str to obtain string contents of the ostringstream cout << "outputString contains:\n" << outputString.str(); return 0;
}</source>
outputString contains: string 123.457220x22fe74"
Save string with ostringstream
<source lang="cpp">#include <iostream>
- include <sstream>
using namespace std; int main(int argc, char** argv) {
cout << "Enter tokens. Control-D (Unix) or Control-Z (Windows) to end." << endl; ostringstream outStream; while (cin) { string nextToken; cout << "Next token: "; cin >> nextToken; if (nextToken == "done") break; outStream << nextToken << "\t"; } cout << "The end result is: " << outStream.str();
}</source>