Incorrect reading of floating point

Asked

Viewed 98 times

1

I have a file that stores products, each product has three information: its name (char *), quantity in stock (int) and its price (float). The information generated in my program I am saving in a file and leaving the file with the following format:

quantidade-de-produtos
nome-produto-1
em-estoque-1 preco-1
nome-produto-2
em-estoque-2 preco-2
...

Example:

1
Refrigerante Xingu
50 3.500000 

Since I’m scrutinizing the file, obviously I’m also reading it, and here’s the problem. I’m doing the reading with the following code:

fscanf(dados, "%d ", &quantidade);
for (struct produto p; quantidade > 0; quantidade--){
    fscanf(dados, "%[^\n]*c", p.nome);
    fscanf(dados, "%d%f", &p.EmEstoque, &p.preco);
    ...

Done this, if I read in the example given above, it will read all the items correctly, until it reaches the price. The reading of the price that should be 3.500000, becomes only 3, and the . 500000 is left aside and the reading closed.

What’s wrong with my code?

Product struct statement if you need to better understand the code:

struct produto {
    char nome[100];
    int EmEstoque;
    float preco;
};

3 answers

3

Since you didn’t show all your code I wrote a small code that reads from a text file with the same formatting that your code reads.

file. c

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

int main(){
   FILE *fp;
   int qtd,estoque;
   char nome[100];
   float preco;

   fp = fopen("texto.txt", "r"); // read mode
   if (fp == NULL){
       perror("Error while opening the file.\n");
       exit(EXIT_FAILURE);
   }
   qtd=0;
   fscanf(fp, "%d ", &qtd);
   for (; qtd > 0; qtd--){
       fscanf(fp, "%[^\n]*c", nome);
       fscanf(fp, "%d %f", &estoque, &preco);
       printf("\n%s %d %f \n \n",nome,estoque,preco);
   }

   fclose(fp);
   return 0;
}

text.txt

1
nome
5 3.5

exit from the program

nome 5 3.500000 

In this way, I can think that the problem in your code should be in one of the parts that you omitted, it is also worth mentioning that if you used some blibioteca or artificial to change the formatting of the text, it may require different entries like using ',' instead of '.' to float

  • C and C++ programs are not called "scripts", a term that is usually only applied to interpreted languages such as Python and Perl

  • I hadn’t thought of it that way, but I’ll fix it, thanks for the warning.

0


Problem solved.

The problem is not read, it is write. Reading a float to, when finding a '.', the correct character must be a ','.

-1

Friend, use the "dot" in the directive, in your case "%. 2f" instead of getting 3.500000 as you wish, would be a real value of 3.50, without formatting with "dot" in the directive, when printar will come only the "int" value of the data.

  • Before I already did the post, I tested this, and this is not the problem. Still, thank you.

  • Ahh beauty, already tried to use only the float directive, in its written code, is using the two in union '%d%f' fscanf(data, "%d","%f", &p.Emestoque, &p.preco);

  • I found the error, read my answer in this same question.

  • I understood, still yes, it is strange to use the ',' incidentally depends on some library that included right, but understood. Rsrs even more

Browser other questions tagged

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