1
I’m making a program that basically takes a float, calculates a percentage under the value if you want to record, will record the information in a txt to be incremented now (basically used as a database). The problem comes in the following respect. When deleting the txt file and running the program (which checks if the file exists, if it does not create it), the problem arises that after creation, there is a fprintf() that prints default values to be used, but nothing is printed in the file until the 2 run(the first error and closes the program, obviously). Code from within the main opening and creation of the archive.
arq = fopen("dados.txt","r+");
if (arq == NULL){
arq = fopen("dados.txt","wb");
printf("Arquivo \"dados.txt\" inexistente!\nCriando novo arquivo.\n\n");
fprintf(arq, "Terreno %2.2f\n",Terreno);
fprintf(arq, "CNH %2.2f\n",CNH);
fprintf(arq, "Contas %2.2f\n",Contas);
fprintf(arq, "Poupança %2.2f\n",Poupanca);
fprintf(arq, "Lazer %2.2f\n",Lazer);
fprintf(arq, "Utilitários %2.2f\n",Utilitarios);
}
And now the final fscan() and fprintf code, which displays in the program.
printf("-==Exibição de valores armazenados==-\n");
printf("+----------------------+\n");
fscanf(arq, "%s %f", TextoAux, &AcmTerreno);
AcmTerreno += Terreno;
printf("|%s = %8.2f|\n", TextoAux,AcmTerreno);
fscanf(arq, "%s %f", TextoAux, &AcmCNH);
AcmCNH += CNH;
printf("|%s = %8.2f|\n", TextoAux,AcmCNH);
fscanf(arq, "%s %f", TextoAux, &AcmContas);
AcmContas += CNH;
printf("|%s = %8.2f|\n", TextoAux,AcmContas);
fscanf(arq, "%s %f", TextoAux, &AcmPoupanca);
AcmPoupanca += Poupanca;
printf("|%s = %8.2f|\n", TextoAux,AcmPoupanca);
fscanf(arq, "%s %f", TextoAux, &AcmLazer);
AcmLazer += Lazer;
printf("|%s = %8.2f|\n", TextoAux,AcmLazer);
fscanf(arq, "%s %f", TextoAux, &AcmUtilitarios);
AcmUtilitarios += Utilitarios;
printf("|%s = %8.2f|\n", TextoAux,AcmUtilitarios);
printf("+----------------------+\n");
fprintf(arq, "Terreno %2.2f\n",AcmTerreno);
fprintf(arq, "CNH %2.2f\n",AcmCNH);
fprintf(arq, "Contas %2.2f\n",AcmContas);
fprintf(arq, "Poupança %2.2f\n",AcmPoupanca);
fprintf(arq, "Lazer %2.2f\n",AcmLazer);
fprintf(arq, "Utilitários %2.2f\n",AcmUtilitarios);
Thanks for the help! I will adjust the code to do what I want and test, at first it looks like it will improve(and much) what I want to do.
– Gabriel Guerra