Include spaces when reading C strings

Asked

Viewed 103 times

0

Good staff

I have a C job to do, where I have to read information from a txt. Each string follows the following format:

12345António Manuel Silva Mendes                        Frankfurt  Varsovia    1

It always has a 5-character integer for identification, 51 characters for the name (including spaces), 11 for the city of departure, 11 for the city of arrival and a maximum 2-digit integer for the date.

declare the structure thus:

typedef struct bilhete{

    int BI;
    char nome[51];
    char partida[11];
    char chegada[11];
    int data;

}BILHETE;

and I’m reading from the file like this:

while(!feof(fp)){

        fscanf(fp,"%d%51c%11c%11c%d\n", &bilhete.BI, bilhete.nome, bilhete.partida, bilhete.chegada, &bilhete.data);

I happen to be doing a debug printf of the ticket.name, and it’s not printing properly. How to recognize white spaces?

1 answer

0

Try using %[ n]s, for example: In the format expression, instead of using %51s for "string", use %51[ n]s.

  • Thank you, you already have. However the ticket.departure and the ticket.arrival are not being printed. Any suggestions?

Browser other questions tagged

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