-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileStorage.cpp
More file actions
102 lines (86 loc) · 2.88 KB
/
Copy pathfileStorage.cpp
File metadata and controls
102 lines (86 loc) · 2.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Created by haris on 12/28/2025.
//
#include "fileStorage.h"
#include<iostream>
#include<fstream>
#include<filesystem>
#include<string>
#include<limits>
std::string readMultilineNote(const char* prompt) {
std::cout << prompt << "\n"
<< "(Type ::end on a new line to finish)\n";
std::string note, line;
while (true) {
std::getline(std::cin, line);
if (line == "::end") break;
note += line;
note += "\n";
}
// remove trailing newline (optional)
if (!note.empty() && note.back() == '\n') note.pop_back();
return note;
}
int main() {
std::string filename;
while (true) {
std::cout << "\n----- Note Application -----\n"
<< "1) Add a note\n"
<< "2) View notes\n"
<< "3) Clear notes\n"
<< "4) Exit\n"
<< "Choice: ";
int choice;
if (!(std::cin >> choice)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input.\n";
continue;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (choice == 1) {
std::filesystem::path new_dir_path = "C:\\Users\\haris\\OneDrive\\Desktop\\notes";
std::filesystem::current_path(new_dir_path);
std::cout << "Current working directory changed to: " << std::filesystem::current_path() << std::endl;
std::cout << "Label your file: ";
std::getline(std::cin, filename);
std::ofstream out(filename, std::ios::app);
if (!out) {
std::cout << "Error opening file.\n";
continue;
}
std::string notes = readMultilineNote("Enter your note:");
if (!notes.empty()) {
out << "----- NOTE START -----\n";
out << notes << "\n";
out << "----- NOTE END -----\n";
std::cout << "Saved.\n";
} else {
std::cout << "Empty note skipped.\n";
}
}
else if (choice == 2) {
std::ifstream in(filename);
if (!in) {
std::cout << "No notes found.\n";
continue;
}
std::string line;
std::cout << "\n--- Notes ---\n";
while (std::getline(in, line)) {
std::cout << line << "\n";
}
}
else if (choice == 3) {
std::ofstream out(filename, std::ios::trunc);
std::cout << "Notes cleared.\n";
}
else if (choice == 4) {
break;
}
else {
std::cout << "Unknown option.\n";
}
}
return 0;
}