0

I can't find the error in this function,it only writes 0's on my .txt file. It is supposed to read the scores from teams like (2 x 1) team 2 beats team 1 or (2 - 1) team 2 and 1 have a deuce. In the "seek" function,the 3 first characters of the file are "t g",in which t = teams and g = games. I've tried many approaches but none of them worked...

void calcular_resultados(char nome_campeonato[],int vitorias[50],int derrotas[50],int empates[50])
{
int retornador = 1,times=0;
FILE *arq;
FILE *resultados;
strcpy(results,nome_campeonato);
strcat(results," - Resultados.txt");
if(arq = fopen(campeonato,"r")) /* abre o arquivo de campeonato para leitura */
{
    int t1,t2;
    char x,barraene;

    resultados = fopen(results,"w"); /* abre o arquivo de resultados para ESCRITA */
    fscanf(arq,"%d",&times);
    rewind(arq);
    for(i=0;i<times;i++)
        vitorias[i] = 0;
    for(i=0;i<times;i++)    //Zera os valores de cada vitoria para serem incrementados
        derrotas[i] = 0;
        for(i=0;i<times;i++)    //Zera os valores de cada derrota para serem incrementados
        empates[i] = 0;
        fseek(arq,3,SEEK_SET);
    while(eof(campeonato) == 0)
    {
        printf("entrou no while\n");
        int posicao;
        fscanf(arq,"%d %c %d",&t1,&x,&t2); /* faz o scan de uma "partida" */
        posicao = ftell(resultados);
        printf("posicao atual: %d\n",posicao);
        if(x == 'x')
        {
            vitorias[t1]++;
            derrotas[t2]++;
        }else if(x == '-')
        {
            empates[t1]++;
            empates[t2]++;
        }
    }
    fclose(resultados);
    resultados = fopen(results,"w");
    for(i=0;i<times;i++)
    {
        fprintf(resultados,"%d %d %d\n",vitorias[i],derrotas[i],empates[i]);
    }


}else
{
    printf(ARQUIVO_N_ENCONTRADO);
}
    fclose(resultados);
    fclose(arq);

}
  • There is not `eof` function in standard library. And [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Eugene Sh. Aug 02 '18 at 19:13
  • Gotta check that out... Still I believe that might not be the main problem,since the values are actually peing printed but incorrectly. – João Victor Vilar Aug 02 '18 at 19:23
  • 1
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, but more urgently, please read about how to create an MCVE ([MCVE]). You might just need to create a `main()` function that defines some variables and calls the code currently shown in the question. We also need to see a minimal sample data file that shows the problem. And then we need to see the output you get and the output you expect. (There seems to be a global variable `results` that you've not shown (unless you translated `resultados` to `results` except in the declaration), though…we need compilable code!) – Jonathan Leffler Aug 02 '18 at 19:32
  • You should check that you open files successfully every time (twice, not just once). You should print errors to standard error, not standard output. You should not close files if you failed to open them — your calls to `fclose()` are in the wrong place if you fail to open either file. – Jonathan Leffler Aug 02 '18 at 19:45
  • Check the return value of `fopen()`, `fscanf()`, `fseek()`, `ftell()` and the problem will be apparent. – chux - Reinstate Monica Aug 02 '18 at 20:28

0 Answers0