1 Replies - 851 Views - Last Post: 02 December 2011 - 04:37 AM Rate Topic: -----

#1 ssh2pd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-December 11

Loading a file into a linked list

Posted 01 December 2011 - 10:42 PM

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?

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


:code:

This post has been edited by jimblumberg: 01 December 2011 - 10:46 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Loading a file into a linked list

#2 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Loading a file into a linked list

Posted 02 December 2011 - 04:37 AM

Welcome :). First of all please attempt to compile and run your program as there are obvious errors within it. If you're stuck with fixing any of the reported errors, let us know what they are and show the relevant code. Preferably post complete but minimal examples for us to work with. One hint is that you're not allocating memory, you need to use malloc (and free when you're done with it).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1