Loading a File into a linked list
Hi guys. I'm not really understanding how to do linked lists. I'm attempting to load a file into a linked list but I'm getting confused with the different pointers. Am I on the right track? What lines should I look over and change?
Code:
struct transaction
{
char itemName[20];
int itemNumber, itemAmount;
float itemPrice;
struct transaction *nextPtr;
};
typedef struct transaction Item;
/*function LoadFile*/
Item* LoadFile(char*inputFile)
{
int i;
Item* tempPtr;
Item* prevPtr;
Item* currentPtr;
File *fPtr = fopen(inputFile, "r");
while (!feof(fPtr))
{
printf("File has been loaded\n");
scanf(fPtr, "%d", &itemNumber);
currentPtr = NULL;
prevPtr = NULL;
for (i = 0; i<itemNumber; i++)
{
fscanf(fPtr, "%d", &(temp->itemNumber));
fscanf(fPtr, "%s", temp->itemName);
fscanf(fPtr, "%d", &(temp->itemAmount));
fscanf(fPtr, "%d", &(temp->itemPrice));
if(currentPtr == NULL)
{
currentPtr = temp;
}
if(prevPtr != NULL)
{
prevPtr->nextPtr = tempPtr;
}
tempPtr = tempPtr->nextPtr;
}
fclose(fPtr)
}