-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.cpp
84 lines (66 loc) · 2.02 KB
/
files.cpp
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
/*
* Universidade Federal do Rio de Janeiro
* Escola Politecnica
* Departamento de Eletronica e Computacao
* EEL670 - Linguagens de Programacao - Turma 2020/2
* Prof. Miguel Elias M. Campista
* Autor: Luiz Carlos Aguiar Carrion & Maria Eduarda de Souza Mafra
* Trabalho Final - Etapa 2
* Decricao:
*
*/
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "files.h"
#include "product.h"
using namespace std;
void fileWrite (string fileName, vector <Product*> productList) {
ofstream myFile (fileName);
for (size_t index = 0; index < productList.size(); index++) {
string name = (*(productList[index])).getName();
replace(name.begin(), name.end(), ',', '.');
myFile << name << ","
<< (*(productList[index])).getPrice() << ","
<< (*(productList[index])).getURL() << ","
<< (*(productList[index])).getStore() << endl;
}
myFile.close();
}
void fileRead (string fileName, vector <Product*> &productList) {
ifstream myFile (fileName);
string currentLine;
while (getline(myFile, currentLine)) {
int startName = 0;
int endName, startPrice, endPrice;
int startUrl, endUrl, startStore;
int endStore = currentLine.size() - 1;
int current = 1;
size_t index = 0;
while (index < currentLine.size() && current < 4) {
if (currentLine[index] == ',') {
if (current == 1) {
endName = index;
startPrice = index + 1;
} else if (current == 2) {
endPrice = index;
startUrl = index + 1;
} else if (current == 3) {
endUrl = index;
startStore = index + 1;
}
current ++;
}
index++;
}
string name = currentLine.substr (startName, endName - startName);
string price = currentLine.substr (startPrice, endPrice - startPrice);
string url = currentLine.substr (startUrl, endUrl - startUrl);
string store = currentLine.substr (startStore, endStore - startStore + 1);
Product* newProduct = new Product (name, price, url, store, true);
productList.push_back (newProduct);
}
myFile.close();
}