0

I want to read data from a FILE and save it to a linked-list and my problem is apparently with the reading command "fscanf".

I'm trying to make a function that receives a head of a linked-list and a pointer to a file. The function reads the data from the file and saves them into a node then connects the node to the beginning of the linked list, i.e. to the head, not to the end.

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

#define NameLength 15

typedef struct Product {
    char ProductName[NameLength];
    int Quantity;
    int Price;
    char Premium;
}Product;

typedef struct ProductList {
    Product P;
    struct ProductList* next;
}ProductList;

void DeleteList(ProductList*);
void ErrorMsg(const char*);
int CheckInList(ProductList*, const char*);

void CreateProducts(ProductList *head, FILE *fp) {
    ProductList *temp = (ProductList*)malloc(sizeof(ProductList));
    //If the dynamic memory allocation for temp has failed, print a 
message and exit the program.
    if (!temp) {
        DeleteList(head);
        ErrorMsg("Error: Memory allocation of temp in CreateProducts has 
failed.");
     }

    temp->next = NULL;
    while (!feof(fp)) {
        fscanf(fp, "%s%d%d%c", temp->P.ProductName, &temp->P.Quantity, 
&temp->P.Price, temp->P.Premium);
        if (CheckInList(head, temp->P.ProductName))
            printf("Error: Product is already found in the list.\n");
        else {
            if (temp->P.Quantity < 0)
            printf("Error: Quantity of the product cannot be 
negative.\n");
            else {
                if (temp->P.Price < 0)
                    printf("Error: Price of the product cannot be 
negative.\n");
                else
                {
                    //Adding the product to the beginning of the list 
every time.
                    if (head == NULL)
                        head = temp;
                    else
                    {
                        temp->next = head->next;
                        head->next = temp;
                    }
                }
            }
        }
    }
    if (head != NULL)
        printf("Products' information have been received.\n");
    else
        ErrorMsg("Products' information have NOT been received.");
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

1 Answers1

5

Turn on your compiler's warnings! It will literally give you the answer.

main.cpp: In function 'void CreateProducts(ProductList*, FILE*)':
main.cpp:33:20: warning: format '%c' expects argument of type 'char*', but argument 6 has type 'int' [-Wformat=]
         fscanf(fp, "%s%d%d%c", temp->P.ProductName, &temp->P.Quantity,
                    ^~~~~~~~~~
 &temp->P.Price, temp->P.Premium);
                 ~~~~~~~~~~~~~~~

(I know it says main.cpp; that's just an artefact of the online compiler, which I put into C mode.)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Hey, I'm kinda new to this, so I don't really know what you're talking about when you say "Turn on your compiler's warnings", how do I do that? I couldn't possibly miss it if it was so obvious. Thank you for taking the time to answer my question. – Tarek M. Mousa Jun 17 '19 at 15:20
  • @RishabhRyber Hey, I'm very sorry for the very late reply. Yes, it did solve the problem. – Tarek M. Mousa Jul 10 '19 at 11:34
  • @TarekM.Mousa Great! You can go ahead and [accept it](https://stackoverflow.com/help/someone-answers). :) – Lightness Races in Orbit Jul 10 '19 at 11:48