Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
istringstream.cpp
Go to the documentation of this file.
1
29#include <iomanip> // for setw
30#include <ios> // for dec, hex, oct, right, showbase
31#include <iostream> // for endl, wcout, wostream
32#include <locale> // for isspace()
33#include <sstream> // for strstream, wistringstream
34#include <string> // for wstring
35
36#include <examples.h>
37
38
39int main ( )
40{
41 try {
42
43 // create a read/write stream object
44 std::wistringstream in (std::ios::in | std::ios::out);
45
46 // associate the ostream object's buffer with the ifstream object
47 std::wostream out (in.rdbuf ());
48
49 // enable exceptions in both streams
50 in.exceptions (std::ios::badbit);
51 out.exceptions (std::ios::failbit | std::ios::badbit);
52
53 // write string into out
54 out << L"Il avait l'air heureux." << std::endl;
55
56 // seek to the beginning of the stream
57 in.seekg (0);
58
59 wchar_t c;
60
61 // output each space-separated word on a separate line
62 while (in.get (c)) {
63 if ((std::isspace)(c, in.getloc ()))
64 std::wcout << std::endl;
65 else
66 std::wcout.put(c);
67 }
68
69 std::wcout << std::endl;
70
71 // clear stream state, failbit | eofbit set by last call to get ()
72 in.clear ();
73
74 // move back to the beginning of the stream
75 in.seekg (0);
76
77 wchar_t buf [40];
78
79 // guard against buffer overflow
80 in.width (sizeof buf);
81
82 // set right justification
83 std::wcout << std::right;
84
85 // does the same thing as the previous code
86 // output each word on a separate line in a field of width 10
87 while (in >> buf) {
88 std::wcout.width (10);
89 std::wcout << buf << std::endl;
90 }
91
92 std::wcout << std::endl;
93
94 // clear flags, last in >> buf set fail bit
95 // because of a newline at end of string
96 in.clear ();
97
98 // output the base info before each integer
99 out << std::showbase;
100
101 std::wostream::pos_type pos= out.tellp ();
102
103 const long l = 10;
104
105 // output l in hex with a field with of 10
106 out << std::hex << std::setw (10) << l << std::endl;
107
108 // output l in oct with a field with of 10
109 out << std::oct << std::setw (10) << l << std::endl;
110
111 // output l in dec with a field with of 10
112 out << std::dec << std::setw (10) << l << std::endl;
113
114 // move back to the beginning of the stream
115 in.seekg (0);
116
117 // output the entire stream
118 std::wcout << in.rdbuf () << std::endl;
119
120 // clear the flags
121 in.clear ();
122
123 // seek the input sequence to pos
124 in.seekg (pos);
125
126 in.unsetf (std::ios::basefield);
127
128 int a, b, d;
129
130 // read the previous outputted integer
131 in >> a >> b >> d;
132
133 // output 3 times 10
134 std::wcout << a << std::endl << b << std::endl << d << std::endl;
135
136 }
137 catch (std::ios::failure &e) {
138 std::wcerr << e.what () << std::endl;
139 }
140
141 return 0;
142}
#define d
Definition 17.names.cpp:115
#define e
Definition 17.names.cpp:118
#define a
Definition 17.names.cpp:109
#define buf
Definition 17.names.cpp:452
#define c
Definition 17.names.cpp:111
#define b
Definition 17.names.cpp:110
#define pos
Definition 17.names.cpp:518
const int out
const int in
int main()