How to pass information from a file to a dynamic vector of C structures?

0

Good afternoon! (EDITED) I'm doing a work in C language, where I have to display the information stored in a text file in the console, but it is mandatory to pass all information to% dynamic% of structures. I think I've already done something, but when I try to save the specialty in vetores , the file ends up saving the next words.

Ex: variável is saving "Joao Silva" and v->nome is saving "Neurology 9.30 - 17.00" and I intend to save only "Neurology". Can someone help me?

struct hora_entrada{int horas, minutos;};

struct hora_saida{int horas, minutos;};

typedef struct medico med, *p_med;
struct medico{
char nome[ST_TAM];
char especialidade[ST_TAM];
struct hora_entrada h_e;
struct hora_saida h_s;
};

int le_dados (){
FILE *f, *g;
med *v;
f=fopen("medico.txt", "rt");
g=fopen("paciente.txt", "rt");
if (f==NULL || g==NULL){
    printf("Erro no acesso ao ficheiro.\n");
    return 0;
}
v=malloc(sizeof(med));
if (v==NULL){
    printf("Erro na alocaçao de memoria.\n");
    return 0;
}
while ((fscanf(f,"%49[^\n] %49[^\n]",v->nome, v->especialidade))==2 )
        printf("%s\n%s",v->nome, v->especialidade);
fclose(f);
}

    
asked by anonymous 07.08.2017 / 18:08

1 answer

0

I'd rather use fgets instead of scanf to read rows from a file ( link ).

while (fgets(v->nome, 49, f)){
        fgets(v->especialidade, 49, f);
        fgets(aux, 49, f); // Lê os horários //
        printf("%s%s", v->nome, v->especialidade);
}

You can read the name and specialty with fgets and times with fscanf, if you prefer.

    
09.08.2017 / 16:25