How do I read and separate the save data separated by the period and comma and comma?

Asked

Viewed 134 times

-2

Good evening guys, I am making a program in c that has to read a file and save the data, and each line of the file represents the data of a client, how do I save the data by separating it by these ";" and ","? The file to be read is:

txt file. 1 ; Familiar ; 2 ; 2 ; I 3 , M 3 , BR 4 , AL 2 2 ; Individual ; 1 ; I 2 , M 2 3 ; Familiar ; 1 ; 0 ; AB 2 , AM 3 , AL 5 , BR 2 4 ; Individual ; 0 ; AL 3 , BR 2

1 answer

0

See an example here:

Dados ficheiro:  
emilio silva ; 25 , 2 , 2017
carla patricia , 3 , 4 , 2016
Silvia Pereira ; 25 , 8 , 2015
Miguel Antonio, 7 , 8 , 2014  

Code:

int main(void){

    FILE *fp = fopen("testo.txt", "r");

    char linha[100];
    int dia, mes, ano;
    char nome[50], apelido[50];

    if(fp != NULL) {

        while(fgets(linha, 100, fp)){

            sscanf(linha, "%s %s ; %d , %d , %d", nome, apelido, &dia, &mes, &ano);

            printf("\n%s %s, %d %d %d\n", nome, apelido, dia, mes, ano);
        }
    } else {
        printf("\nErro");
    }


    system("pause");
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.