-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
54 lines (49 loc) · 1.66 KB
/
Copy pathFileHandler.cpp
File metadata and controls
54 lines (49 loc) · 1.66 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
#include "FileHandler.h"
#include <sstream>
string name = "stock.csv";
/* Save to file function loops through the queue that's passed in to add entries to a csv */
void FileHandler::saveToFile(Queue* stock) {
ofstream output(name);
if(output.is_open()) {
Node* curr = stock->front;
while(curr != nullptr) {
output << curr->id << "," << curr->name << "," << curr->category << "," << curr->quantity << "," << curr->price << "," << curr->dateIn << "," << curr->dateOut << endl;
curr = curr->next;
}
}
}
/* Load from file uses a stringstream to parse one line at a time, then using getlines and
* with a delimiter we feed a temporary node each one of its fields (converted to their approriate types).
* Result is then enqueued onto the queue we return.
*/
Queue FileHandler::loadFromFile() {
ifstream input(name, ios::app);
Queue stock;
string line;
while(getline(input, line)) {
Node *node = new Node();
stringstream ss(line);
string field;
getline(ss, field, ',');
node->id = field;
getline(ss, field, ',');
node->name = field;
getline(ss, field, ',');
node->category = field;
getline(ss, field, ',');
node->quantity = field;
getline(ss, field, ',');
node->price = field;
getline(ss, field, ',');
node->dateIn = field;
getline(ss, field, ',');
node->dateOut = field;
stock.enqueue(node);
}
return stock;
}
void FileHandler::setPassword(string password) {
ofstream createPass("password.txt",ios::out);
createPass << password;
createPass.close();
}