-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvManager.cpp
More file actions
60 lines (55 loc) · 1.54 KB
/
csvManager.cpp
File metadata and controls
60 lines (55 loc) · 1.54 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
//
// Created by razoc on 01/10/2025.
//
#include "csvManager.h"
#include <sstream>
void CSVFile::add_product(const Product &p){
this->products.push_back(p);
}
void CSVFile::print() const {
for (const auto& product : products) {
std::cout << "ID: " << product.id << std::endl;
std::cout << "Name: " << product.name << std::endl;
std::cout << "Price: " << product.price << std::endl;
std::cout << std::endl;
}
}
void CSVFile::write(const string& filename) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cout << "Error al abrir el archivo";
return;
}
file << "id, name, price \n";
for (const auto& product : products) {
file << product.id;
file << "," << product.name;
file << "," << product.price;
file << std::endl;
}
}
void CSVFile::read(const string& filename, bool header) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cout << "Error al abrir el archivo";
return;
}
string line;
while (std::getline(file, line)) {
char delimiter = ',';
if (header) {
header = false;
continue;
}
std::istringstream ss(line);
string id_str, name, price_str;
std::getline(ss, id_str, delimiter);
std::getline(ss, name, delimiter);
std::getline(ss, price_str, delimiter);
Product p;
p.id = std::stoi(id_str);
p.name = name;
p.price = std::stod(price_str);
products.push_back(p);
}
}