-1
I need to read a file with data where an integer is the product ID on a line, the product name on the next line and the product authorizer on the line following the product name. Totaling 3 lines for each product.
Example of Txt file with 3 products:
7891058005221
AMARYL 4MG
SANOFI – PROGRAMA VIVA FUNCIONAL
7896015529086
ANORO
VIVER MAIS – GSK FUNCIONAL
7896637017046
ARPADOL 60CP
PROGRAMA SOU MAIS VIDA – APSEN FUNCIONAL
I want the user to enter the code in Numbers and the program to find the product and write the name of the product and the program to which it belongs.
#include<stdio.h>
#include<locale.h>
#include<stdbool.h>
struct Item{
int EAN;
char Medicamento[30];
char Programa[100];
};
main()
{
setlocale(LC_ALL,"");
struct Item Item;
bool achou = false;
int EAN_P;
char Buscador[30];
FILE *BancoMed;
BancoMed = fopen("BancoMed.txt","r");
if(BancoMed == NULL)
{
printf("\n Erro ao abrir! ");
close();
}
do{
printf("\n Código de barra / 0 para sair: ");
scanf("%d",&EAN_P);
do
{
fscanf(BancoMed,"%d\n",&Item.EAN);
if(Item.EAN == EAN_P)
achou = true;
fgets(Item.Medicamento,"%s",BancoMed);
fgets(Item.Programa,"%s",BancoMed);
}while(fgets(Buscador,"%s",BancoMed)!=NULL && achou!=true );
printf("\n %s\n %s",Item.Medicamento,Item.Programa);
}while(EAN_P!=0);
fclose(BancoMed);
}
The problem is that it prints the last item, never the middle or the first.
You are specifying the parameters of the fgets function incorrectly. The parameters are: Pointer to a char array to which the string will be read; Maximum number of characters to be copied; Pointer to a FILE type object.
– anonimo
I do not believe that the problem is there, because even changing the Char quantity parameter it still can not pass the conditional, it reads until the end of the file and always prints the last 3 lines (ID, Product and Authorizer)
– Rodolfo Antunes