Include spaces when reading strings in C

0

Good people

I have a C job to do, in which I have to read information from a lot of different txt.com entries. Each string follows the following format:

12345António Manuel Silva Mendes                        Frankfurt  Varsovia    1

There is always an integer of 5 characters for the ID, 51 characters for the name (including spaces), 11 for the city of departure, 11 for the arrival and an integer with a maximum of 2 digits for the date. p>

I declare the structure like this:

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);

It turns out that I'm doing a debug printf of the ticket.name, and it is not printing correctly. How do I recognize white space?

    
asked by anonymous 06.12.2017 / 18:31

1 answer

0

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

    
06.12.2017 / 19:08