int storage in c

Asked

Viewed 139 times

-1

I’m starting in C and making a program that records some information (all this information goes to a file). each record has a registration number, starting with the record 0001. The problem is that I am trying to store the number of the last record made in another file, to know what will be the next registration number so that each time I register a complaint the number of the previous claim is the number + 1 link pro full code http://dontpad.com/codigoint

FILE *total;
numero_reclamacao = 1;
total = fopen("total.txt", "a+b");

            if(total){
                while(!feof(total)){
                    fscanf(total , "%d", &numero_reclamacao);//lendo o ultimo
                }                                            //n de reclamacao
            }
            else{
                printf("ERRO");
            }

numero_reclamacao += 1;//incrementando + 1           

registrar(numero_reclamacao,outros parametros);//funcao que registra

fprintf(total, "%d", numero_reclamacao);//escrevendo no arquivo o último n de 
                                         //reclamacao usado

fclose(total);

what is happening is that whenever I register a complaint her number is 1, it never goes to 2, 3, 4, etc

  • If it is a text file why you specify binary (option b) in fopen?

  • it was just to test, but with or without the b I have the same problem

  • first thing: fix code indentation...with sloppy indentation becomes more difficult to understand...plus the code is incomplete...not that it is to put 500 lines, but at least the full function, and/or the full main function would help more understanding

  • What is the content of the file after at least one run?

  • link to full code http://dontpad.com/codigoint

  • the contents of the file were to be 1 in the first run, 2 in the second, 3 in the third, etc

  • I did not ask what was to be but rather the actual contents of the file after some executions.

  • stays always what was already there, if I put 10, will always register the complaint 10 and will never record 11 in the file, if there is nothing in the file gets always 0, never increases

Show 3 more comments

1 answer

0

If fscanf is running, then the contents of the file where you store the last record are blank or not being read properly... and fscanf ends up writing the value 0 in the variable. Because of this, the variable is always set to 1 after increment.

Browser other questions tagged

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