Help with strings read from C file

0

Well I'm creating a static list TAD, a log file with 4 strings must be read and inserted into the list.

Problem encountered in reading the file, where it is described to read ; after jumping a character and so on, however when it reads, remains of previous strings in other strings.

Note: Looking at the image makes it easier to understand what is happening

typedef struct LOGIN{
    char user[max];
    char date_login[10];
    char time_on[5];
    char time_out[5];
}login;

typedef struct LISTA{
    login log[max];
    int start,end;
}lista;


lista* cria_lista(){
    lista *l = (lista *)malloc(sizeof(lista));
    return(l);
}


void inicia_vazia(lista *l){
    l->start = 0;
    l->end = 0;
}

void insere(lista *l, login *log){
    if(l->end == max){
        exit(1);
    }
    strcpy(l->log[l->end].user,log->user);
    strcpy(l->log[l->end].date_login,log->date_login);
    strcpy(l->log[l->end].time_on,log->time_on);
    strcpy(l->log[l->end].time_out,log->time_out);
    l->end++;
}

void insere_lista_arquivo(lista *l){
    char url[10];
    login log;
    strcpy(url,"login.log");
    FILE *arquivo = fopen(url,"r");
    if(arquivo == NULL){
        printf("Arquivo não encontrado!\n");
    }
    else{
        do{
            fscanf(arquivo,"%[^;]s",log.user);
            fgetc(arquivo);
            fscanf(arquivo,"%[^;]s",log.date_login);
            fgetc(arquivo);
            fscanf(arquivo,"%[^;]s",log.time_on);
            fgetc(arquivo);
            fscanf(arquivo,"%[^\n]s",log.time_out);
            insere(l,&log);
            fgetc(arquivo);
            if(feof(arquivo)){
                break;
            }
        }while(!feof(arquivo));
        fclose(arquivo);
    }
}

void mostra_lista(lista *l){
    for(int i = l->start; i < l->end - 1; i++){
        printf("User: %s / Date_login: %s / Time_on: %s /Time_out:%s\n\n",l->log[i].user,l->log[i].date_login,l->log[i].time_on,
        l->log[i].time_out);
    }
    printf("\n");
}


int main(){
    lista *l = cria_lista();
    inicia_vazia(l);
    insere_lista_arquivo(l);
    mostra_lista(l);
    return(0);
}

    
asked by anonymous 13.04.2018 / 04:36

0 answers