-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.cpp
More file actions
54 lines (51 loc) · 1.08 KB
/
files.cpp
File metadata and controls
54 lines (51 loc) · 1.08 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
#include <iostream>
#include <fstream>
#include <string>
class Test {
std::fstream fstream;
std::ifstream ifstream;
std::ofstream ofstream;
std::istringstream istringstream;
std::ostringstream ostringstream;
std::stringstream str;
};
void Write() {
std::ofstream out{"data.txt"};
out << "Hello world" << std::endl;
out << 10 << std::endl;
out.close();
}
void Read() {
std::ifstream input{ "data.txt" };
//if (!input.is_open()) {
// std::cout << "Could not open the file" << std::endl;
// return;
//}
if (input.fail()) {
std::cout << "Could not open the file" << std::endl;
return;
}
std::string message;
std::getline(input, message);
int value{};
input >> value;
input >> value;
if (input.eof()) {
std::cout << "End of file encountered" << std::endl;
}
if (input.good()) {
std::cout << "I/O operations are successful" << std::endl;
}
else {
std::cout << "Some I/O operations failed" << std::endl;
}
input.setstate(std::ios::failbit);
input.clear();
input.close();
std::cout << message << ":" << value << std::endl;
}
int main() {
Write();
Read();
return 0;
}