-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_creation.h
More file actions
68 lines (60 loc) · 2.21 KB
/
Copy pathtask_creation.h
File metadata and controls
68 lines (60 loc) · 2.21 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
#include<iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
class Taskcreation {
public:
void createTask();
private:
void WriteFile(const std::string& content, const std::string& expire_date, const std::string& priority);
int getNextTaskID();
};
int Taskcreation::getNextTaskID() {
ifstream infile("Task.txt");
if (!infile.is_open()) return 1; // Primo task
string line;
int lastID = 0;
while (getline(infile, line)) {
if (line.rfind("Task ID:", 0) == 0) { //if the line starts with "Task ID:"
try {
int id = stoi(line.substr(8)); // Prende il numero dopo "Task ID:"
if (id > lastID) lastID = id;
} catch (...) {
continue;
}
}
}
infile.close();
return lastID + 1;
}
//WriteFile opens the Task.txt file writes the content and close it afterwards
void Taskcreation::WriteFile( const std::string& content, const std::string& expire_date, const std::string& priority) {
int id = getNextTaskID();
ofstream file("Task.txt", std::ios::app); // Create and open the file
time_t now = time(0);
int i=0;
if (file.is_open()) {
string timestamp = ctime(&now);
timestamp.pop_back(); // Remove newlin
file <<"Task ID:"<<id<<" "<< content << " "; // Write content to the file
file << "current date at the creation: " << timestamp << " "; // Write current timestamp to the file
file << "task expiring date: "<< expire_date << " "; // Write expire date to the file
file << "priority: " << priority << "\n";
file.close(); // Close the file
cout << "File '" << "Task.txt" << "' created and written successfully.\n";
} else {
std::cerr << "Failed to create or open the file.\n";
}
}
void Taskcreation::createTask() {
string content, expire_date, priority;
cout << "Enter task content: ";
getline(cin, content);
cout << "Enter task expire date (YYYY-MM-DD): ";
getline(cin, expire_date);
cout << "Enter task priority (Low, Medium, High): ";
getline(cin, priority);
WriteFile(content,expire_date,priority);
std::cout << "Task created!"<< std::endl;
}