fprintf() C error when printing, value displayed(-1,#R) other than being allocated(0)

Asked

Viewed 90 times

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);

1 answer

1


The following are two examples based on your racecinio line.

1) Reading/Saving 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;
}
  • 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.

Browser other questions tagged

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