Calculate how long a car has been parked

5

I'm reading a parque.txt file that contains the hours a car was in a parking lot, type "9h00 10h30" , this on each line.

I was trying to get these times in each char block, in which case I would have hora_i="9", min_i="00", hora_f="10", min_f="30" , the problem is that in printf it shows hora_i="9", min_i="009", hora_f="10009", min_f="3010009" .

I've tried to fflush(stdin) , but it did not work ...

void calcula_estacionamento()
{
    FILE *f;
    int n=0,i=0;
    char linha[MAX],hora_i[2]="",min_i[2]="",hora_f[2]="",min_f[2]="";
    f=fopen("parque.txt","r");
    while(fgets(linha,MAX,f)!=NULL)
    {
        while(linha[n]!='h')
        {
            hora_i[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!=' ')
        {
            min_i[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!='h')
        {
            hora_f[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!='
void calcula_estacionamento()
{
    FILE *f;
    int n=0,i=0;
    char linha[MAX],hora_i[2]="",min_i[2]="",hora_f[2]="",min_f[2]="";
    f=fopen("parque.txt","r");
    while(fgets(linha,MAX,f)!=NULL)
    {
        while(linha[n]!='h')
        {
            hora_i[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!=' ')
        {
            min_i[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!='h')
        {
            hora_f[i]=linha[n];
            n++;
            i++;
        }
        i=0;
        n++;
        while(linha[n]!='%pre%')
        {
            min_f[i]=linha[n];
            n++;
            i++;
        }
        printf("\n%s %s %s %s",hora_i,min_i,hora_f,min_f);
        n=0;
        i=0;
    }
}
') { min_f[i]=linha[n]; n++; i++; } printf("\n%s %s %s %s",hora_i,min_i,hora_f,min_f); n=0; i=0; } }
    
asked by anonymous 15.06.2014 / 02:11

1 answer

5

Missing the null characters ( %code% ) in each of its strings. That is, each of your vectors must have up to 3 characters:

char linha[MAX], hora_i[3] = "", min_i[3] = "", hora_f[3] = "", min_f[3] = "";

It's important that you end every string correctly while reading your code

hora_i[i] = '
while(linha[n]!='
f=fopen("parque.txt","r");
if(f == NULL) {
    perror("Erro abrindo o arquivo");
    return(-1);
}
' && linha[n]!='\n') { // ...
'; i=0; // ... min_i[i] = '
fclose(f);
'; i=0; // ... hora_f[i]= '
char linha[MAX], hora_i[3] = "", min_i[3] = "", hora_f[3] = "", min_f[3] = "";
'; i=0; // ... min_f[i]= '
hora_i[i] = '
while(linha[n]!='
f=fopen("parque.txt","r");
if(f == NULL) {
    perror("Erro abrindo o arquivo");
    return(-1);
}
' && linha[n]!='\n') { // ...
'; i=0; // ... min_i[i] = '
fclose(f);
'; i=0; // ... hora_f[i]= '%pre%'; i=0; // ... min_f[i]= '%pre%';
';

Finally, if you really want to read multiple lines of a file, it is important to predict both the end of the file and the end of a line:

%pre%

Functional example in Ideone

Important points I could not include in the example

Missing error handling when opening file:

%pre%

As well as the treatment to close it:

%pre%     
15.06.2014 / 03:21