fprintf () C error when printing, displayed value (-1, # R) other than the one to be allocated (0)

0

I'm doing a program that basically takes a float, calculates a percentage under the value if you want to write, it will write the information in a txt to be incremented now (basically used as a database). The problem comes in the following aspect. When deleting the txt file and running the program (which verifies that the file exists, if it does not create it), the problem arises that after creation, there is a fprintf () that printed default values to be used, but nothing is printed on file up to 2 execution (the first of the error and closes the program, obviously). Code from inside the main of opening the file and creating it.

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);
    
asked by anonymous 12.07.2017 / 02:10

1 answer

0

Here are two examples based on your line of ratiocination.

1) Read / Write in text mode:

#include <stdio.h>
#include <stdlib.h>

typedef struct registro_s
{
    double terreno;
    double CNH;
    double contas;
    double poupanca;
    double lazer;
    double utilitarios;
} registro_t;


size_t gravar_registro( const char * arq, registro_t * rec )
{
    FILE * fp = fopen( arq, "w" );

    if(!fp)
        return -1;

    fprintf( fp, "%lf %lf %lf %lf %lf %lf\n", rec->terreno, rec->CNH, rec->contas, rec->poupanca, rec->lazer, rec->utilitarios );

    fclose(fp);

    return 0;
}


size_t ler_registro( const char * arq, registro_t * rec )
{
    FILE * fp = fopen( arq, "r" );

    if(!fp)
        return -1;

    fscanf( fp, "%lf %lf %lf %lf %lf %lf\n", &rec->terreno, &rec->CNH, &rec->contas, &rec->poupanca, &rec->lazer, &rec->utilitarios );

    fclose(fp);

    return 0;
}


void exibir_registro( registro_t * rec )
{
    printf("Terreno    : %2.2f\n", rec->terreno );
    printf("CNH        : %2.2f\n", rec->CNH );
    printf("Contas     : %2.2f\n", rec->contas );
    printf("Poupanca   : %2.2f\n", rec->poupanca );
    printf("Lazer      : %2.2f\n", rec->lazer );
    printf("Utilitários: %2.2f\n", rec->utilitarios );
}


int main( int argc, char * argv[] )
{
    registro_t a;
    registro_t b;

    /* Preenche registro A com os dados */
    a.terreno = 1.123;
    a.CNH = 2.468;
    a.contas = 9.999;
    a.poupanca = 0.1;
    a.lazer = 0.0;
    a.utilitarios = 100.10;

    /* Grava registro A no arquivo */
    gravar_registro( "teste.txt", &a );

    /* Le conteudo do arquivo e preenche registro B */
    ler_registro( "teste.txt", &b );

    /* Exibe registro B */
    exibir_registro( &b );

    return 0;
}

2) Read / Write in binary mode:

#include <stdio.h>
#include <stdlib.h>

typedef struct registro_s
{
    double terreno;
    double CNH;
    double contas;
    double poupanca;
    double lazer;
    double utilitarios;
} registro_t;


size_t gravar_registro( const char * arq, registro_t * rec )
{
    size_t nbytes = 0L;
    FILE * fp = fopen( arq, "wb" );

    if(!fp)
        return -1;

    nbytes = fwrite( rec, sizeof(registro_t), 1, fp );

    fclose(fp);

    return nbytes;
}


size_t ler_registro( const char * arq, registro_t * rec )
{
    size_t nbytes = 0L;
    FILE * fp = fopen( arq, "rb" );

    if(!fp)
        return -1;

    nbytes = fread( rec, sizeof(registro_t), 1, fp );

    fclose(fp);

    return nbytes;
}


void exibir_registro( registro_t * rec )
{
    printf("Terreno    : %2.2f\n", rec->terreno );
    printf("CNH        : %2.2f\n", rec->CNH );
    printf("Contas     : %2.2f\n", rec->contas );
    printf("Poupanca   : %2.2f\n", rec->poupanca );
    printf("Lazer      : %2.2f\n", rec->lazer );
    printf("Utilitários: %2.2f\n", rec->utilitarios );
}


int main( int argc, char * argv[] )
{
    registro_t a;
    registro_t b;

    /* Preenche registro A com os dados */
    a.terreno = 1.123;
    a.CNH = 2.468;
    a.contas = 9.999;
    a.poupanca = 0.1;
    a.lazer = 0.0;
    a.utilitarios = 100.10;

    /* Grava registro A no arquivo */
    gravar_registro( "teste.bin", &a );

    /* Le conteudo do arquivo e preenche registro B */
    ler_registro( "teste.bin", &b );

    /* Exibe registro B */
    exibir_registro( &b );

    return 0;
}
    
12.07.2017 / 16:21