-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.cpp
73 lines (57 loc) · 1.74 KB
/
convert.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
#include "arquivo.h"
#include <sstream>
/*
Modos de abertura de arquivos:
wb -> Escrita
rb -> leitura
ab -> anexar
rb+ -> leitura e escrita
wb+ -> leitura e escrita (apaga o conteúdo caso o arquivo exista)
ab+ -> leitura e escrita (adiciona ao final do arquivo)
*/
using namespace std;
int contador = 0;
void transformaEmBinario(Arquivo, char arq[]); // Tranforma as informações lidas em binário
void transformaEmBinario(Arquivo dados, char arq[]) {
FILE *file = fopen(arq, "ab");
if(file){
fwrite(&dados, sizeof(Arquivo), 1, file);
fclose(file);
}
else
printf("\nErro ao abrir arquivo!\n");
}
vector<string> split(const string &str, char sep)
{
vector<string> tokens;
string token;
stringstream ss(str);
while (getline(ss, token, ',')) {
token.erase(remove_if(token.begin(), token.end(), ::isspace), token.end());
if (!token.empty()) {
tokens.push_back(token);
}
}
return tokens;
}
int main() {
ifstream arquivo("Subnational-period-life-tables-2017-2019-CSV.csv");
Arquivo dados;
string tempString;
string linha;
char arquivoEscrita[] = {"subnational.dat"};
while(getline(arquivo, linha)){
char sep = ',';
vector<string> tokens = split(linha, sep);
strcpy(dados.measure,tokens[0].data());
strcpy(dados.quantile,tokens[1].data());
strcpy(dados.area,tokens[2].data());
strcpy(dados.sex,tokens[3].data());
strcpy(dados.age,tokens[4].data());
strcpy(dados.geography,tokens[5].data());
strcpy(dados.ethnic,tokens[6].data());
strcpy(dados.value,tokens[7].data());
transformaEmBinario(dados, arquivoEscrita);
}
return 0;
}