include fstream include string class CLogger private std fstream file

 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
#include <fstream>
#include <string>
class CLogger {
private:
std::fstream file;
public:
CLogger(const char *pathname) { file.open(pathname, std::fstream::out); }
~CLogger() { if (file.is_open()) file.close(); };
//вывод для сишных строк
bool operator << (const char* msg) {
if (file.is_open())
file << msg << std::endl;
else return false;
return true;
}
//вывод для c++ string
bool operator << (std::string& msg) {
if (file.is_open())
file << msg << std::endl;
else return false;
return true;
}
} log("file.txt");
int main()
{
log << "This is c string";
std::string str = "This is c++ std::string";
log << str;
return 0;
}