I'm having trouble saving the read data from a text file and a vector from a structure I created.
My goal is to open a text file, grab the data that is saved in it, and save it to a vector of type INFO. My program compiles perfectly, but when I put to print the data of each vector position, all fields, minus registration and salary, are empty.
I tried to send the address of the salvaDados(&pessoa)
vector but that also went wrong.
So, kindly ask someone to show me how to permanently record the data in my vector. Thanks =)
void salvaDados(INFO pessoa[]) {
FILE* f;
char linha[200];
f = fopen("teste.txt", "r");
if(f == NULL){
printf("Desculpa, banco de dados indisponível\n");
exit(0);
}
// O fseek faz com que a barra se seleção pule para local indicado;
//Nesses caso, a barra irá pular 49 bytes a partir da linha inicial(SEEK_SET);
fseek(f, 49, SEEK_SET);
int i = 0;
while((fscanf(f,"%s", linha)) != EOF) {
char* tok;
tok = strtok(linha, ",");
while(tok != NULL) {
sscanf(tok, "%d", &(pessoa[i].matricula));
tok = strtok(NULL, ",");
pessoa[i].nome = tok;
tok = strtok(NULL, ",");
pessoa[i].sobrenome = tok;
tok = strtok(NULL, ",");
pessoa[i].email = tok;
tok = strtok(NULL, ",");
pessoa[i].telefone = tok;
tok = strtok(NULL, ",");
sscanf(tok, "%f", &(pessoa[i].salario));
tok = strtok(NULL, ",");
}
i++;
}
}
My struct has been defined as follows:
typedef struct informacoes{
int matricula;
char* nome;
char* sobrenome;
char* email;
char* telefone;
float salario;
}INFO;