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 to a file and leaving the file in 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 am writing the file, obviously I am also doing your reading, and this is where the problem is . I'm 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);
...
Once this is done, if I read in the example given above, it will read all the items correctly until the price is reached. The reading of the price should be 3,500,000, becomes only 3, and the .500000 is left out and the reading closed.
What's wrong with my code?
Product struct declaration if you need to better understand the code:
struct produto {
char nome[100];
int EmEstoque;
float preco;
};