Loading a File into a linked list

This is a discussion on Loading a File into a linked list within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I'm not really understanding how to do linked lists. I'm attempting to load a file into a linked ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

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

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    There are some problems. for example:
    itemNumber is a class member, the line
    Code:
    scanf(fPtr, "%d", &itemNumber);
    is wrong.
    you need to allocate memory for one Item and then use its data to put stuff in.
    e.g.

    Code:
    Item * tempPtr = (Item * )malloc (sizeof(Item));
    if (tempPtr == NULL){
       // do some error;
    }
    and then when you want to use you should do:
    Code:
    scanf(fPtr, "%d", &(tempPtr->itemNumber));
    Also, you assume a lot about the file content structure, be careful..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list from file
    By kapil1089thekin in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 12:23 PM
  2. Linked List to file
    By RoseGirl in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 03:05 PM
  3. file into a linked list
    By pilecom in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 12:14 AM
  4. Saving and Loading Linked Lists With File i/o
    By Red Army in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2002, 03:19 PM
  5. file i/o -linked list
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21