-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInputOutputStreams.cpp
More file actions
221 lines (186 loc) · 6.68 KB
/
InputOutputStreams.cpp
File metadata and controls
221 lines (186 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// =====================================================================================
// InputOutputStreams.cpp // Input/Output Streams
// =====================================================================================
module modern_cpp:input_output_streams;
namespace InputOutputStreams {
static void test_01() {
// input of several values with one statement
int val1, val2;
std::cout << "Please enter 2 int-values:";
std::cin >> val1 >> val2;
std::cout << val1 << " : " << val2 << std::endl;
}
static void test_02() {
// output with put and write - unformatted output
std::string s("ABC");
for (char ch : s) {
std::cout.put(ch);
}
std::cout << 10 << std::endl;
std::cout.put(10) << std::endl; // is newline '\n'
char charData[] = { '1', '2', '3', '4' };
std::cout.write(charData, 4);
std::cout.put(10) << std::endl; // is newline '\n'
std::cout.flush(); // is newline '\n'
}
void checkIOstate(std::ios& stream) {
if (stream.good()) {
std::cout << "Everything Okay" << std::endl;
}
else if (stream.bad()) {
std::cout << "Fatal Error" << std::endl;
}
else if (stream.fail()) {
std::cout << "Error during Input/Output" << std::endl;
if (stream.eof()) {
std::cout << "EOF reached" << std::endl;
}
}
stream.clear();
}
static void test_03() {
int val = 0;
std::cout << "Enter value: ";
std::cin >> val;
checkIOstate(std::cin);
//= enter value - everything shold be okay, if you enter an int value
std::ifstream file;
file.open("notExisting.txt");
checkIOstate(file);
//= error during IO
std::fstream fstr;
fstr.open("newFile.txt", std::ofstream::out | std::ofstream::in | std::ofstream::binary | std::ofstream::trunc);
fstr << "this is some text in this file\n";
fstr.seekp(std::ios_base::beg);
char ch;
while (fstr.good()) {
fstr.get(ch);
if (fstr.good())
std::cout.put(ch);
}
checkIOstate(fstr);
//= error during IO
//= EOF reached
}
void f() {
bool b = true;
std::cout << b << std::endl; // output: "true"
}
static void test_04() {
bool b = true;
std::cout << std::boolalpha << b << std::endl; // output: "true"
b = false;
std::cout << b << std::endl; // output: "false"
f();
std::cout << std::noboolalpha << b << std::endl; // output: "0"
b = true;
std::cout << b << std::endl; // output: "1"
}
void g() {
int val = 100;
std::cout << val << std::endl; // output: 0x64
}
static void test_05() {
int val = 255;
std::cout << std::showbase << std::endl;
std::cout << std::dec << val << std::endl; // output: 255
std::cout << std::hex << val << std::endl; // output: 0xff
g();
std::cout << std::oct << val << std::endl; // output: 0377
std::cout << val << std::endl; // output: 0377
std::cout << std::dec;
std::cout << val << std::endl; // output: 255
}
static void test_06() {
int val = -123;
std::cout << std::setw(10) << std::internal << val << std::endl;
//= - 123
std::cout << std::setw(10) << std::left << val << std::endl;
//= -123
std::cout << std::setw(10) << std::right << val << std::endl;
//= -123
val = 987;
std::cout << std::setw(10) << std::internal << val << std::endl;
//= 987
std::cout << std::setw(10) << std::left << val << std::endl;
//= 987
std::cout << std::setw(10) << std::right << val << std::endl;
//= 987
}
static void test_07() {
// note: setw must be caller for every output seperately
int val = 12345;
std::cout << std::setw(10);
std::cout << std::right << val << std::endl;
std::cout << std::right << val << std::endl;
std::cout << std::right << val << std::endl;
std::cout << std::setw(20);
std::cout << std::right << val << std::endl;
std::cout << std::right << val << std::endl;
}
static void test_08() {
// note: setfill must be reset
int val = 12345;
std::cout << std::setw(10) << std::setfill('.');
std::cout << std::right << val << std::endl;
std::cout << std::right << val << std::endl;
std::cout << std::setw(20);
std::cout << std::right << val << std::endl;
std::cout << std::right << val << std::endl;
std::cout << std::setw(10) << std::setfill(' ');
std::cout << std::right << val << std::endl;
}
// writing manipulators without parameters
std::ostream& hash(std::ostream& os) {
os << '#';
return os;
}
auto hashLambda = [](std::ostream& os) -> std::ostream& {
return os << "#";
};
static void test_09() {
std::cout << "Some Text " << hash << " Some Text" << std::endl;
std::cout << "some text " << hashLambda << " some text" << std::endl;
}
// writing manipulators with parameters
// here: putting n exclamation marks into the output stream
class exmarks {
private:
int m_numExMarks;
public:
exmarks(int n = 1) : m_numExMarks{ n } {}
std::ostream& operator()(std::ostream& os) const { // Functor
for (int i = 0; i < m_numExMarks; ++i)
os << '!';
// return os << '\n';
return os;
}
};
std::ostream& operator<<(std::ostream& os, const exmarks& elem) {
return elem(os);
}
static void test_10() {
std::cout << "AAA" << exmarks() << "AAA" << std::endl;
std::cout << "AAA" << exmarks(1) << "AAA" << std::endl;
std::cout << "AAA" << exmarks(2) << "AAA" << std::endl;
std::cout << "AAA" << exmarks(3) << "AAA" << std::endl;
std::cout << "AAA" << exmarks(4) << "AAA" << std::endl;
}
}
void main_input_output_streams()
{
using namespace InputOutputStreams;
// test_01(); // needs console input
test_02();
// test_03(); // needs console input
test_04();
test_05();
test_06();
test_07();
test_08();
test_09();
test_10();
}
// =====================================================================================
// End-of-File
// =====================================================================================