Read File with Integers and Strings in C

Asked

Viewed 42 times

-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.

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

1 answer

0


Problem Solved

do
        {
            fscanf(BancoMed,"%d\n",&Item.EAN);
            if(Item.EAN == EAN_P)
            {
                fgets(Item.Medicamento,"%s",BancoMed);
                fgets(Item.Programa,"%s",BancoMed);
                break;
            }
        }while(fgets(Buscador,"%s",BancoMed)!=NULL && achou!=true );

In the while-block where you test if I found the ID, I made a block where if it finds the integer ID and compare to the request it stores the required strings.

  • Where you put this "%s" you have to put the maximum number of characters to be read. See the fgets function documentation.

  • So, as I commented above, this was not the problem, because even using the Read string "%s" parameter, it usually works by capturing the strings needed for the program’s goal. Still thanks for your attention.

Browser other questions tagged

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