I am having difficulty assigning read values from a file in csv format to one struct, my file has the following structure:
1;República Checa;156.9
2;Irlanda;131.1
3;Alemanha;115.8
4;Austrália;109.9
5;Áustria;108.3
And my code looks like this:
struct Nodo{
int classificacao;
char pais[30];
float consumo;
};
function that reads the csv file:
void leArquivoDeDados() {
int length;
char * buffer;
struct Nodo nodo[35];
ifstream is;
is.open ("dados.csv", ios::in);
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
if (is.is_open())
{
int n = 0;
buffer = new char[length];
is.read(buffer,length);
if (length > 0)
{
while(n < length) //isso substitue o ";" para um espaço
{
n++;
if (buffer[n] == ';') buffer[n] = '\t';
}
printf("%s", buffer);
getch();
delete []buffer;
}
}
else {
printf("Arquivo não foi aberto");
getch();
}
is.close();
}
I am declaring my struct as a vector of 35 positions and I want to assign the values before each semicolon, for example:
Nodo.classificacao = 1;
Nodo.pais = 'Irlanda';
Nodo.consumo = 156.9;
My function is able to write to the terminal my file, but I do not know how to accomplish this assignment in the above example at the time of reading the file, I am replacing the ";" by spaces only to visualize better in the terminal.