-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyDataTxtFiles.cpp
116 lines (89 loc) · 2.01 KB
/
CopyDataTxtFiles.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
class FileCopy {
public:
void copyData(string fileNameRead, string fileNameWrite);
void logFile(string message);
bool FileRead(string fileNameRead);
void FileWrite(string fileNameWrite);
private:
time_t seconds = time(NULL);
tm* timeinfo = localtime(&seconds);
string fileLog = "d:\\log.txt";
string tempString;
};
int main() {
setlocale(LC_ALL, "ru");
string file1, file2, fileLog;
file1 = "d:\\test1.txt";
file2 = "d:\\test2.txt";
FileCopy valera;
valera.copyData(file1, file2);
cout << endl << endl;
system("pause>null");
}
void FileCopy::copyData(string fileNameRead, string fileNameWrite)
{
if (FileRead(fileNameRead))
{
FileWrite(fileNameWrite);
logFile("Copy is complete");
}
}
bool FileCopy::FileRead(string fileNameRead)
{
ifstream fileRead;
string buffer;
fileRead.open(fileNameRead);
if (fileRead.is_open())
{
logFile("FileRead is open");
while (!fileRead.eof()) {
getline(fileRead, buffer);
tempString += buffer + "\n";
}
}
else
{
logFile("FileRead is not open!"); // FileRead is not open
return false;
}
fileRead.close();
logFile("FileRead closed");
return true;
}
void FileCopy::FileWrite(string fileNameWrite)
{
ofstream fileWrite;
fileWrite.open(fileNameWrite);
if (fileWrite.is_open())
{
logFile("FileWrite is open");
fileWrite << tempString;
}
else
{
logFile("FileWrite is not open!"); // FileWrite is not open
}
fileWrite.close();
logFile("FileWrite closed");
}
void FileCopy::logFile(string addMessage)
{
ofstream fileWrite;
fileWrite.open(fileLog, ios_base::app);
if (fileWrite.is_open())
{
fileWrite << asctime(timeinfo) << " " << addMessage << endl;
cout << "Â ëîã-ôàéëå ñîçäàíà çàïèñü!" << endl;
}
else
{
cout << "Error! Log file is not open" << endl;
cout << addMessage;
}
}