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;
};
C and C++ programs are not called "scripts", a term that is usually only applied to interpreted languages such as Python and Perl
– zentrunix
I hadn’t thought of it that way, but I’ll fix it, thanks for the warning.
– Junior Nascimento