/
streams.cpp
86 lines (78 loc) · 2.24 KB
/
streams.cpp
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
#include <iostream>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
void testStreams(std::istream& is = std::cin, std::ostream& os = std::cout) {
int a = 0, b = 0, c = 0;
is >> a;
os << "a = " << a << std::endl;
is >> b;
os << "b = " << b << std::endl;
// os << "\rerror\n";
is >> c;
os << "c = " << c << std::endl;
const int MAX = 100;
char s[MAX];
// !!! is >> s[0];
is.get(); // изчистване на символа за нов ред
is.get(s, MAX);
os << strlen(s) << " -> '" << s << "'" << std::endl;
is.getline(s, MAX);
os << strlen(s) << " -> '" << s << "'" << std::endl;
}
void testStringStreams() {
std::istringstream iss("1 2 3\nHello\nHello");
std::ostringstream oss;
testStreams(iss, oss);
std::cout << "Прихванахме изхода на testStreams():\n" << oss.str() << std::endl;
}
void testFileStreams() {
const int MAX = 100;
char s[MAX];
std::cout << "Въведете име на входен файл: ";
std::cin.getline(s, MAX);
std::ifstream ifs(s);
std::cout << "Въведете име на изходен файл: ";
std::cin.getline(s, MAX);
std::ofstream ofs(s);
testStreams(ifs, ofs);
}
int safeReadInt(char const* prompt = "", std::istream& is = std::cin) {
int x;
bool success = false;
while (!success) {
std::clog << "state = " << is.rdstate() << std::endl;
std::cout << prompt;
is >> x;
std::clog << "state = " << is.rdstate() << std::endl;
if (!is) {
// операции по възстановяване на потока
is.clear();
// прочитаме един символ и го изхвърляме
is.get();
} else
success = true;
}
return x;
}
void testState() {
int a = 0, b = 0, c = 0;
// !!! std::cin.clear(std::ios::badbit);
std::clog << "state = " << cin.rdstate() << std::endl;
std::cin >> a;
std::clog << "state = " << cin.rdstate() << std::endl;
std::cin >> b;
std::cin >> c;
a = safeReadInt("a = ");
std::cout << "a = " << a << std::endl;
std::cout << "end\n";
std::cout << std::setfill('-') << std::setw(40) << '-' << std::endl;
}
int main() {
// testStringStreams();
testFileStreams();
// testState();
return 0;
}