1
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, take the data that is saved in it, and save in an array of type INFO. My program compiles perfectly, but when I print the data of each vector position, all fields, less enrollment and salary, are empty.
I tried to send the vector address salvaDados(&pessoa)
but that also went wrong.
So I kindly ask someone to show me how to permanently record the data in my vector.
Thank you =)
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 was defined as follows:
typedef struct informacoes{
int matricula;
char* nome;
char* sobrenome;
char* email;
char* telefone;
float salario;
}INFO;
In this case, if you read without this NULL will not work? try to pass to struct with the auxiliary variables. Try there to see if it works.
– Gustavo Feliciano