0

I'm trying to develop a solution for my algoritms and data structures subject. I have a function that takes a student through a comparison of the number read by a number that exists in my database (corresponds to the number of lines).

Happens right then the system enter the meal, which is only 1 for each day of each week. So far, no problems.

The system checks the balance of the student, and if the student has 0.0 balance or less than 3 balance (which is the cost of each meal) the system prompts or automatically recommends loading the balance, and mostly do all the steps correctly.

It turns out, that maybe due to several implemented cycles, I have almost everything to go well but at the end of the function instructions the instructions remove(filename); andrename("backup.c",filename);`

CODE:

void EncomendaRefeicoes()
{
    FILE *myFile;

    FILE *fin, *fout;//ficheiro de entrada ficheiro de saida 
    const char *fn = "database-aluno.txt";
    myFile = fopen(filename, "r");
    int num; //numero
    rewind(myFile);
    printf("Insrira o numero de aluno");//Ask of number
    scanf("%d", &num);//read number
    printf("\n A verificar se o aluno existe!");//Check
    //procurar estudante
    int fnum = 0;//student number in file
    struct Aluno student;
    char fnom[50];//student file name
    char saldotostring[5];//
    double saldo;//saldo 
    //int i=0;

    while (!(feof(myFile)))//enquanto difrente de fim de ficheiro(feof)
    {
        fscanf(myFile, "%d %s %s %lf %d-%d\n", &Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, &Aluno[acstruct].saldo, &Aluno[acstruct].dia, &Aluno[acstruct].mes);
        //file scan as variaveis num,name,saldo,dia,mes

        if (num == Aluno[acstruct].num) //se num lido == num ficheiro
        {
            printf("\n");
            printf("\n");
            printf("Aluno %d encontrado", num);//Dizer que foi encontrado
            printf("\n");
            printf("Numero: %d", Aluno[acstruct].num);//imprime numero
            printf("\n");
            printf("Nome: %s", Aluno[acstruct].name);//imnprime nome
            printf("\n");
            printf("Subnome: %s", Aluno[acstruct].fname);
            printf("\n");
            //sprintf(saldotostring, "%.2f", saldo);
            printf("Saldo: %.2f", Aluno[acstruct].saldo);//imprime saldo
            printf("\n");
            printf("Custo de aquisicao: 3.00 euros \n");//diz qual o custo de aquisicao
            printf("\n");



            //Menu
            SelectOneFood(6, fp1);
            printf("\n");
            printf("Quer enconendar? \n");

            printf("1-Reservar \n");
            printf("\n");
            printf("2-Mandar-me para o menu principal \n");
            int opcaoescolhida;
            scanf("%d", &opcaoescolhida);
            double price = 3.00;

            if (opcaoescolhida == 1)
            {
                if (Aluno[acstruct].saldo == 0.0)
                {

                    char op = 'N';
                    printf("Nao tem saldo. Deseja carregar o plafound? (Primir N para nao) \n");
                    /*
                    if (scanf("%c", op) == 'N')
                    {
                    _tmain();
                    }
                    */
                    printf("Valor a inserir: ");
                    printf("\n");

                    scanf("%lf", &saldo);
                    if (saldo < 5.00)
                    {
                        printf("Minimo de carregamento obrigatorio e de 5 euros \n");
                        scanf("%lf", &saldo);

                    }
                    fin = fopen(fn, "r");
                    fout = fopen("sbackup.txt", "w");//an temporary file
                    rewind(fin);
                    rewind(fout);
                    while (!(feof(fin))){
                        //Lê 1 a 1
                        fscanf(fin, "%d %s %s %lf %d-%d\n", &Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, &Aluno[acstruct].saldo, &Aluno[acstruct].dia, &Aluno[acstruct].mes);

                        if (num == Aluno[acstruct].num)
                            fprintf(fout, "%d %s %s %lf %d-%d\n", Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, saldo, Aluno[acstruct].dia, Aluno[acstruct].mes);
                        else if (num != Aluno[acstruct].num)

                            fprintf(fout, "%d %s %s %lf %d-%d\n", Aluno[acstruct].num, Aluno[acstruct].name, Aluno[acstruct].fname, Aluno[acstruct].saldo, Aluno[acstruct].dia, Aluno[acstruct].mes);

                    }

                    remove(filename);
                    rename("sbackup.txt",filename);
                    printf("\n");
                    printf("Saldo alterado");
                    fclose(fout);
                    fclose(myFile);
                    fclose(fin);

                }

                /*
                if (Aluno[acstruct].saldo < price)
                printf("Voce nao tem saldo suficiente \n");
                */
                else if (Aluno[acstruct].saldo >= price)
                {
                    Aluno[acstruct].saldo = Aluno[acstruct].saldo - price;
                    //substiruir saldo no ficheiro de texto

                    printf("Refeicao adequerida com sucesso \n");
                    printf("Saldo Final %.2f euros", Aluno[acstruct].saldo);


                }
            }
            if (opcaoescolhida == 2)
                _tmain();

        }

    }
}

How can I solve this problem? It will be a problem in if and while cycles?

Vitor Ferreira
  • 163
  • 1
  • 3
  • 11
  • [Stop using `feof()` in loop conditions](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). In all but the rarest of exceptions, its plain-wrong, and this is not such an exception. – WhozCraig Dec 30 '14 at 14:08

1 Answers1

0

Why don't you check the return value of remove() and in case of error, check the errno?

IMO, the problem is, you're calling remove() with the pointer of the file which is already fopen()ed and not yet fclose()d.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261