-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.2.4.1 Using files - flags
48 lines (44 loc) · 1.03 KB
/
8.2.4.1 Using files - flags
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
template <class T>
std::string getStatus(const T & stream, string filename) {
ostringstream s;
if (stream.is_open())
{
s << "File: " << filename << " was successfully opened!\n";
}
else
{
s << "Failed to open file: " << filename << endl;
}
return s.str();
}
std::string getFlags(const ios & stream) {
stringstream s;
s << boolalpha << "G:" << stream.good() << " E:" << stream.eof() << " F:" << stream.fail()
<< " B:" << stream.bad();
return s.str();
}
int main() {
string line;
ifstream myfile;
string filename = ("C:/Users/nicky/source/repos/lab8/Debug/text.txt");
myfile.open(filename);
cout << "Status: " << getStatus(myfile, filename) << "Flags: " << getFlags(myfile) << endl;
if (!myfile.is_open()) {
perror("Error open");
//exit(EXIT_FAILURE);
}
int counter = 0;
while (getline(myfile, line)) {
if (counter % 2)cout << line << endl;
++counter;
}
system("pause");
return 0;
}