File I/O and linked lists

This is a discussion on File I/O and linked lists within the C Programming forums, part of the General Programming Boards category; Hello. This is my code: Code: #include <stdio.h> #include <stdlib.h> /* Struct transaction – This struct should define an itemNumber, ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    File I/O and linked lists

    Hello. This is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Struct transaction – This struct should define an itemNumber, and an itemAmount that are ints, itemPrice which is a float, itemName[20] which is a char, and a next pointer of struct transaction.
    Typedef – you should use a typedef to rename the struct type to item. */
    
    typedef struct transaction {
        int itemNumber;
        int itemAmount;
        float itemPrice;
        char itemName[20];
        struct transaction *next;
    } item;
    typedef item *itemPtr;
    
    int printmenu(void);
    itemPtr loadFile(char *file);
    void insertNode(itemPtr head);
    int deleteNode(itemPtr head);
    void writeFile(char *fPtr, itemPtr head);
    void printList(itemPtr head);
    
     // Main – this function should read in 2 command line arguments.  First is the input file’s name.  The second is the output file’s name. It should then load the file and create the linked list. You should then start looping the menu which calls the other functions.
    
    int main (int argc, char *argv[]) {
        if (argc != 3) {
            printf("\nNot enough arguments.\n");
            printf("Run format: ./a.out <inputfile.txt> <outputfile.txt>\n\n");
            return 0;
        }
    
    
        itemPtr startPtr = loadFile(argv[1]);
    
    
        int menu = printmenu();
    
    
        while (menu != 5) { 
            switch (menu) {
                case 1: // add a transaction
                    insertNode(startPtr);
                    menu = printmenu();
                    break;
                case 2: // remove a transaction
                    deleteNode(startPtr);
                    menu = printmenu();
                    break;
                case 3: // print transaction list
                    printList(startPtr);
                    menu = printmenu();
                    break; 
                case 4: // write transactions to file
                    writeFile(argv[2], startPtr);
                    menu = printmenu();
                    break;
                default: 
                    menu = printmenu();
            }
        }
        
        if (menu = 5) { 
            printf("\nProgram terminated.\n\n");        
            return 0; 
        }
    }
    
    
    int printmenu(void) {
        int select;
    
    
        printf("\nSelect what you would like to do: \n");
        printf("(1) Add a transaction\n");
        printf("(2) Remove a transaction\n");
        printf("(3) Print transaction list\n");
        printf("(4) Write transactions to file\n");
        printf("(5) Exit\n");
        printf("\?: ");
        scanf("%d", &select);
    
    
        return select;
    }
    
    // Loadfile – this function will take in a pointer to a filename and will load the data from the file into a linked list. It will then return the head of the list.  The file format should be as follows.  The < > will not be present in the file. <Item number> <Item name> <amount of item> <price of item>
    
    itemPtr loadFile(char *file) { 
        FILE *fPtr;
    
    
        itemPtr head = malloc(sizeof(item)); // head of linked list
        itemPtr newPtr = malloc(sizeof(item)); // pointer to new node
        itemPtr tail = malloc(sizeof(item)); // tail of linked list
    
    
        int i;
        int noinputs;    
    
    
        fPtr = fopen(file, "r");
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        fscanf(fPtr, "%d", &noinputs);
    
    
        for (i = 0; i < noinputs; i++) {
            if (i == 0) { 
                head = newPtr;
                newPtr->next = NULL;
                tail = newPtr;
             } else { 
                itemPtr newPtr = malloc(sizeof(item));
                tail->next = newPtr;
                fscanf(fPtr, "%d %s %d %lf", &newPtr->itemNumber, newPtr->itemName, &newPtr->itemAmount, &newPtr->itemPrice);
                newPtr->next = NULL;
                tail = newPtr;
            }
        }
        rewind(fPtr);
        fclose(fPtr);
    
    
         return head;
    }
    
    
    // Insert Node – this function should prompt the user for all the inputs needed to add a node to the list, create a new node and attach it to the end of the linked list.  It will take in a pointer to the front of the list.
    
    void insertNode(itemPtr head) { // Check this prototype.
        itemPtr newPtr = malloc(sizeof(item)); // pointer to new node
        itemPtr prevPtr = malloc(sizeof(item)); // pointer to previous node in list
        itemPtr currPtr = head; // pointer to current node in list
            
        printf("\nEnter the item number for the item: ");
        scanf("%d", &newPtr->itemNumber);
        printf("Enter the name for the item: ");
        scanf("%s", newPtr->itemName);
        printf("Enter the price for the item (no $):");
        scanf("%lf", &newPtr->itemPrice);
        printf("Enter the quantity of the item: ");
        scanf("%d", &newPtr->itemAmount);
        newPtr->next = NULL;
        
        while (currPtr->next != NULL) {
            currPtr = currPtr->next;
        }
        if (currPtr->next == NULL) {
            currPtr->next = newPtr;
        }    
    }
    
    
    // Delete Node – delete node will take in the head of the linked list. It will then prompt the user for an itemNumber to look for. If it is not found it will display a message to the user and return to the menu. If it finds it, it will remove the node from the list, keeping the rest of the list intact, and then frees the node.
    
    int deleteNode(itemPtr head) { // Check this prototype.
        int search;
        int found = 0;
        
        itemPtr prevPtr = malloc(sizeof(item)); // pointer to previous node in list
        itemPtr holdPtr = malloc(sizeof(item)); // pointer to new node
        itemPtr currPtr = head; // pointer to current node in list
        
        printf("Enter the transaction number you would like to delete: ");
        scanf("%d", &search);
    
    
        while (currPtr->next != NULL) {
            if (currPtr->itemNumber != search) {
                prevPtr = currPtr;
                currPtr = currPtr->next;
            }
            if (currPtr->itemNumber == search) { 
                found = 1;
                prevPtr->next = currPtr->next;
                holdPtr = currPtr;
                free(holdPtr);
                return;
            }
        }
        
        if (currPtr->next == NULL && found == 0) {
            printf("Item not found!\n");
            return;
        }
    }
    
    
    // Writefile – this function takes in a pointer to a filename for output filename, and the head of the linked list. It will then print all of the information out to the file and close the file.  It should use the same format as Loadfile.
    
    void writeFile(char *file, itemPtr head) {
        FILE *fPtr;
        itemPtr currPtr = head; // pointer to current node in list
        
        // While currPtr->next != NULL, print the items from the linked list into output.txt.
        fPtr = fopen(file, "w+"); // Check this function.
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        while (currPtr->next != NULL) {
            fprintf(fPtr, "%d %s %d %lf", &currPtr->itemNumber, currPtr->itemName, &currPtr->itemAmount, &currPtr->itemPrice);
            fprintf(fPtr, "\n");
            currPtr = currPtr->next;
        }
        rewind(fPtr);
        fclose(fPtr);
    }
    
    
    // printList – this functions takes in a pointer to the head of the list and prints all the information in the list out to the screen. This should use the same format as Loadfile.
    
    void printList(itemPtr head) {
        itemPtr currPtr = head; // pointer to current node in list
        
        printf("\nIn the format: <Item Number> <Item Name> <Quantity> <Price>\n\n");
        
        // while not the end of the list
        while (currPtr->next != NULL) {
            printf("%d\t%s\t%d\t$%.2f\n", currPtr->itemNumber, currPtr->itemName, currPtr->itemAmount, currPtr->itemPrice);
            currPtr = currPtr->next;
        }
    }
    What I have an issue with is loadFile. It doesn't load the file properly, as doesn't scan the itemName and begins with the first line of inputfile.txt. inputfile.txt reads as:

    Code:
    6
    1 widgets 20 0.5
    2 gadgets 250 0.25
    3 whizbangs 1000 2.00
    4 servos 100 3.75
    5 pencils 5000 0.25
    6 hamhawks 15 5.99
    Thanks for your help.
    Last edited by drbr0wn; 11-29-2011 at 01:17 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    Crank up the warnings on your compiler and fix them all:
    Code:
    $ gcc -Wall -g -std=c99  file.c   -o file
    file.c: In function ‘main’:
    file.c:62: warning: suggest parentheses around assignment used as truth value
    file.c: In function ‘loadFile’:
    file.c:114: warning: format ‘%lf’ expects type ‘double *’, but argument 6 has type ‘float *’
    file.c: In function ‘insertNode’:
    file.c:139: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘float *’
    file.c:131: warning: unused variable ‘prevPtr’
    file.c: In function ‘deleteNode’:
    file.c:177: warning: ‘return’ with no value, in function returning non-void
    file.c:183: warning: ‘return’ with no value, in function returning non-void
    file.c: In function ‘writeFile’:
    file.c:198: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
    file.c:198: warning: format ‘%d’ expects type ‘int’, but argument 5 has type ‘int *’
    file.c:198: warning: format ‘%lf’ expects type ‘double’, but argument 6 has type ‘float *’
    • Line 62: A single = is for assignment. A double == is for comparison.
    • Line 114,139: scanf uses "%f" for float and "%lf": for double. You have a float *, so use "%f" or change the float to double. printf doesn't care however, due to argument promotion (a source of confusion for many).
    • Line 117,183: deleteNode doesn't return anything, so declare it to use void (change the prototype too).
    • Line 198: printf doesn't want addresses (except for the "%p" modifier), so drop the & from your parameter list.
    Once you take care of all that, you need to find a good tutorial on linked lists in C. Your handling of nodes other than the first is ok, but the rest of it needs work. Looking at loadFile, here's what I see (my notes are in comments):
    Code:
    itemPtr loadFile(char *file) {
        FILE *fPtr;
    
    
        // allocate 3 nodes without initialization
        // you shouldn't allocate memory here for any node, initialize head and tail to null
        itemPtr head = malloc(sizeof(item)); // head of linked list
        itemPtr newPtr = malloc(sizeof(item)); // pointer to new node
        itemPtr tail = malloc(sizeof(item)); // tail of linked list
    
    
        int i;
        int noinputs;
    
    
        // open the file and read in the number of inputs
        // you should check to make sure fscanf succeeds before continuing
        fPtr = fopen(file, "r");
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        fscanf(fPtr, "%d", &noinputs);
    
    
    
    
        for (i = 0; i < noinputs; i++) {
            if (i == 0) {
                // first node
                // make head point to newPtr (you lose the head you malloc'ed above, a memory leak)
                head = newPtr;
                newPtr->next = NULL;
                // make tail point to newPtr (you lose the tail you malloc'ed above, another memory leak)
                tail = newPtr;
                // this leaves the memory allocated for newPtr as the first element of the list
                // it is not initialized, and you never read in that data from the file
            } else {
                // all other nodes
                // allocate space for the new node (this should be at the top of the loop, outside the if/else)
                // check to make sure malloc succeeded before continuing
                itemPtr newPtr = malloc(sizeof(item));
    
    
    
    
                // append the new node to the end of the list
                tail->next = newPtr;
    
                // read in a line of input into the new node
                // you should make sure fscanf succeeds (returns 4 converted elements) before proceeding
                fscanf(fPtr, "%d %s %d %f", &newPtr->itemNumber, newPtr->itemName, &newPtr->itemAmount, &newPtr->itemPrice);
    
    
                // terminate the list, this can be outside the if/else too, right after the malloc
                newPtr->next = NULL;
    
    
                // set tail to the new end of the list
                tail = newPtr;
    
    
            }
        }
        // close the file, but there is no need to rewind first
        rewind(fPtr);
        fclose(fPtr);
    
    
        // return the start of the list
        return head;
    }
    Last edited by anduril462; 11-29-2011 at 03:48 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Thanks for your previous help. I fixed all the minor errors and brushed up on linked lists some. Below is my refined code. Please, once again, note "loadFile."

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* Struct transaction – This struct should define an itemNumber, and an itemAmount that are ints, itemPrice which is a float, itemName[20] which is a char, and a next pointer of struct transaction.
       Typedef – you should use a typedef to rename the struct type to item. */
    typedef struct transaction {
        int itemNumber;
        int itemAmount;
        float itemPrice;
        char itemName[20];
        struct transaction *next;
    } item;
    
    
    typedef item *itemPtr;
    
    
    int printmenu(void);
    itemPtr loadFile(char *file);
    void insertNode(item *head);
    void deleteNode(item *head);
    void writeFile(char *fPtr, item *head);
    void printList(item *head);
    
    
    // Main – this function should read in 2 command line arguments.  First is the input file’s name.  The second is the output file’s name. It should then load the file and create the linked list. You should then start looping the menu which calls the other functions.  See example for menu.
    int main (int argc, char *argv[]) {
        if (argc != 3) {
            printf("\nNot enough arguments.\n");
            printf("Run format: ./a.out <inputfile.txt> <outputfile.txt>\n\n");
            return 0;
        }
    
    
        itemPtr startPtr = loadFile(argv[1]);
    
    
        int menu = printmenu();
    
    
        while (menu != 5) { 
            switch (menu) {
                case 1: // add a transaction
                    insertNode(startPtr);
                    menu = printmenu();
                    break;
                case 2: // remove a transaction
                    deleteNode(startPtr);
                    menu = printmenu();
                    break;
                case 3: // print transaction list
                    printList(startPtr);
                    menu = printmenu();
                    break; 
                case 4: // write transactions to file
                    writeFile(argv[2], startPtr);
                    menu = printmenu();
                    break;
                default: 
                    menu = printmenu();
            }
        }
        
        if (menu == 5) { 
            printf("\nProgram terminated.\n\n");        
            return 0; 
        }
    }
    
    
    int printmenu(void) {
        int select;
    
    
        printf("\nSelect what you would like to do: \n");
        printf("(1) Add a transaction\n");
        printf("(2) Remove a transaction\n");
        printf("(3) Print transaction list\n");
        printf("(4) Write transactions to file\n");
        printf("(5) Exit\n");
        printf("\?: ");
        scanf("%d", &select);
    
    
        return select;
    }
        
    
    
    // Loadfile – this function will take in a pointer to a filename and will load the data from the file into a linked list. It will then return the head of the list.  The file format should be as follows.  The < > will not be present in the file. <Item number> <Item name> <amount of item> <price of item>
    itemPtr loadFile(char *file) { 
        FILE *fPtr;
    
    
        item *head; // head of linked list
        item *prevPtr;
        item *newPtr; // pointer to new node
    
    
        int i;
        int noinputs;    
    
    
        fPtr = fopen(file, "r");
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        else {
            fscanf(fPtr, "%d", &noinputs);
            printf("\nReading %d inputs into a linked list...\n", noinputs);
        }
    
    
        for (i = 0; i < noinputs; i++) {
            if ((newPtr = malloc(sizeof(item))) == NULL) { printf("Unable to create 'newPtr' node.\n"); }
            if ((prevPtr = malloc(sizeof(item))) == NULL) { printf("Unable to create 'prevPtr' node.\n"); }
            if (i == 0) { 
                head = newPtr;
                newPtr->next = NULL;
             } else { 
                fscanf(fPtr, "%d %s %d %f", &newPtr->itemNumber, newPtr->itemName, &newPtr->itemAmount, &newPtr->itemPrice);
                newPtr->next = NULL;
                prevPtr->next = newPtr;
                prevPtr = newPtr;
            }
        }
        free(newPtr);
        free(prevPtr);
        fclose(fPtr);
    
    
        return head;
    }
    
    
    // Insert Node – this function should prompt the user for all the inputs needed to add a node to the list, create a new node and attach it to the end of the linked list.  It will take in a pointer to the front of the list.
    void insertNode(item *head) { // Check this prototype.
        itemPtr newPtr; // pointer to new node
        itemPtr prevPtr = NULL; // pointer to previous node in list
        itemPtr currPtr = NULL; // pointer to current node in list
    
    
        newPtr = malloc(sizeof(item));
            
        if (newPtr != NULL) {
            printf("\nEnter the item number for the item: ");
            scanf("%d", &newPtr->itemNumber);
            printf("Enter the name for the item: ");
            scanf("%s", newPtr->itemName);
            printf("Enter the price for the item (no $):");
            scanf("%f", &newPtr->itemPrice);
            printf("Enter the quantity of the item: ");
            scanf("%d", &newPtr->itemAmount);
            newPtr->next = NULL;
        
            prevPtr = NULL;
            currPtr = head;
            
            if (currPtr == NULL) {
                head = newPtr;
                newPtr->next = head;        
            } 
            else if (currPtr != NULL) {
                while (currPtr->next != NULL) {
                    currPtr = currPtr->next;
                }
                if (currPtr->next == NULL) { currPtr->next = newPtr; }
            }
        }  else { printf("There is no memory available.\n"); }
    }
    
    
    // Delete Node – delete node will take in the head of the linked list. It will then prompt the user for an itemNumber to look for. If it is not found it will display a message to the user and return to the menu. If it finds it, it will remove the node from the list, keeping the rest of the list intact, and then frees the node.
    void deleteNode(item *head) { // Check this prototype.
        int search;
        int found = 0;
        
        item *prevPtr; // pointer to previous node in list
        item *holdPtr; // pointer to new node
        item *currPtr = head; // pointer to current node in list
        
        printf("Enter the transaction number you would like to delete: ");
        scanf("%d", &search);
    
    
        while (currPtr->next != NULL) {
            if (currPtr->itemNumber != search) {
                prevPtr = currPtr;
                currPtr = currPtr->next;
            }
            if (currPtr->itemNumber == search) { 
                found = 1;
                itemPtr holdPtr = malloc(sizeof(item));
                prevPtr->next = currPtr->next;
                holdPtr = currPtr;
                free(holdPtr);
                return;
            }
        }
        
        if (currPtr->next == NULL && found == 0) {
            printf("Item not found!\n");
            return;
        }
    }
    
    
    // Writefile – this function takes in a pointer to a filename for output filename, and the head of the linked list. It will then print all of the information out to the file and close the file.  It should use the same format as Loadfile.
    void writeFile(char *file, item *head) { // Check this prototype.
        FILE *fPtr;
        item *currPtr = head; // pointer to current node in list
        
        // While currPtr->next != NULL, print the items from the linked list into output.txt.
        fPtr = fopen(file, "w"); // Check this function.
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        while (currPtr->next != NULL) {
            itemPtr currPtr = malloc(sizeof(item));
            fprintf(fPtr, "%d %s %d %lf", currPtr->itemNumber, currPtr->itemName, currPtr->itemAmount, currPtr->itemPrice);
            fprintf(fPtr, "\n");
            currPtr = currPtr->next;
        }
        fclose(fPtr);
    }
    
    
    // printList – this functions takes in a pointer to the head of the list and prints all the information in the list out to the screen. This should use the same format as Loadfile.
    void printList(item *head) {
        item *currPtr = head; // pointer to current node in list
        
        printf("\nIn the format <Item Number> <Item Name> <Quantity> <Price>:\n\n");
        
        // while not the end of the list
        while (currPtr->next != NULL) {
            printf("%d\t%s\t%d\t$%.2f\n", currPtr->itemNumber, currPtr->itemName, currPtr->itemAmount, currPtr->itemPrice);
            currPtr = currPtr->next;
        }
    }
    The error I get is:

    [username@localhost ~]$ ./a.out input.txt output.txt


    Reading 6 inputs into a linked list...
    *** glibc detected *** ./a.out: double free or corruption (fasttop): 0x000000000 0e41430 ***
    ======= Backtrace: =========
    /lib64/libc.so.6[0x3877e7245f]
    /lib64/libc.so.6(cfree+0x4b)[0x3877e728bb]
    ./a.out[0x4009ca]
    ./a.out[0x40075d]
    /lib64/libc.so.6(__libc_start_main+0xf4)[0x3877e1d994]
    ./a.out[0x400669]
    ======= Memory map: ========
    00400000-00402000 r-xp 00000000 00:17 74434723 /studen ts/6/username/a.out
    00601000-00602000 rw-p 00001000 00:17 74434723 /studen ts/6/username/a.out
    00e41000-00e62000 rw-p 00e41000 00:00 0 [heap]
    3877a00000-3877a1c000 r-xp 00000000 fd:00 194698 /lib64/ ld-2.5.so
    3877c1c000-3877c1d000 r--p 0001c000 fd:00 194698 /lib64/ ld-2.5.so
    3877c1d000-3877c1e000 rw-p 0001d000 fd:00 194698 /lib64/ ld-2.5.so
    3877e00000-3877f4e000 r-xp 00000000 fd:00 194714 /lib64/ libc-2.5.so
    3877f4e000-387814e000 ---p 0014e000 fd:00 194714 /lib64/ libc-2.5.so
    387814e000-3878152000 r--p 0014e000 fd:00 194714 /lib64/ libc-2.5.so
    3878152000-3878153000 rw-p 00152000 fd:00 194714 /lib64/ libc-2.5.so
    3878153000-3878158000 rw-p 3878153000 00:00 0
    387de00000-387de0d000 r-xp 00000000 fd:00 194727 /lib64/ libgcc_s-4.1.2-20080825.so.1
    387de0d000-387e00d000 ---p 0000d000 fd:00 194727 /lib64/ libgcc_s-4.1.2-20080825.so.1
    387e00d000-387e00e000 rw-p 0000d000 fd:00 194727 /lib64/ libgcc_s-4.1.2-20080825.so.1
    2ad3b8b7b000-2ad3b8b86000 rw-p 2ad3b8b7b000 00:00 0
    2ad3b8b98000-2ad3b8b99000 rw-p 2ad3b8b98000 00:00 0
    7fffa6772000-7fffa6787000 rw-p 7ffffffe9000 00:00 0 [stack]
    7fffa67fd000-7fffa6800000 r-xp 7fffa67fd000 00:00 0 [vdso]
    ffffffffff600000-ffffffffffe00000 ---p 00000000 00:00 0 [vsysca ll]
    Aborted
    What does this mean? Any ideas? Thank you for your help!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,468
    That error means you're freeing memory you shouldn't be freeing. Sometimes it's because you free a variable that was never initialized/assigned. Sometimes it's the result of freeing the same variable twice. Learn to use a debugger and you will find out that the offending line is this:
    Code:
    free(prevPtr);
    I found it interesting you were trying to free prevPtr. There should never be a call to free in your loadFile function. It's basically an insert function, so you allocate a node and insert it in the list, allocate another node and insert it in the list. You only allocate one node for each list item. You don't allocate newPtr and prevPtr each time, just newPtr. Go brush up on linked lists even more, and perhaps look through your notes and textbooks or Google for some examples.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Thank you, anduril. I do need to brush up on linked lists more and will continue doing so during and after my class is over.

    Code:
    itemPtr loadFile(char *file) {     FILE *fPtr;
    
    
        item *head; // head of linked list
        item *prevPtr;
        item *newPtr; // pointer to new node
    
    
        int i;
        int noinputs;    
    
    
        fPtr = fopen(file, "r");
        if (fPtr == NULL) { printf("\nUnable to open file.\n");    }
        else {
            fscanf(fPtr, "%d", &noinputs);
            printf("\nReading %d inputs into a linked list...\n", noinputs);
        }
    
    
        for (i = 0; i < noinputs; i++) {
            if ((newPtr = malloc(sizeof(item))) == NULL) { printf("Unable to create 'newPtr' node.\n"); }
            if ((prevPtr = malloc(sizeof(item))) == NULL) { printf("Unable to create 'prevPtr' node.\n"); }
            if (i == 0) { 
                head = newPtr;
    /*            printf("%p", head);
                printf("%p", newPtr); works */
                newPtr->next = NULL;
             } else { 
                fscanf(fPtr, "%d %s %d %f", &newPtr->itemNumber, newPtr->itemName, &newPtr->itemAmount, &newPtr->itemPrice);
                newPtr->next = NULL;
    //            printf("%p\n", newPtr->next); works
                printf("%d %s %d %f\n", prevPtr->itemNumber, prevPtr->itemName, prevPtr->itemAmount, prevPtr->itemPrice); // DOES. NOT. WORK.
    //            printf("%d %s %d %f\n", newPtr->itemNumber, newPtr->itemName, newPtr->itemAmount, newPtr->itemPrice); works
                prevPtr->next = newPtr;
    /*            printf("prevPtr->next is %p\n", prevPtr->next);
                printf("newPtr is %p\n\n", newPtr); works */
                prevPtr = newPtr;
    /*            printf("%d %s %d %f\n", prevPtr->itemNumber, prevPtr->itemName, prevPtr->itemAmount, prevPtr->itemPrice);
                printf("%d %s %d %f\n\n", newPtr->itemNumber, newPtr->itemName, newPtr->itemAmount, newPtr->itemPrice); */
            }
        }
        fclose(fPtr);
    
    
    //    printf("%p", head); works
    
    
        return head;
    }
    Why doesn't prevPtr get saved during the iteration?
    Last edited by drbr0wn; 12-01-2011 at 07:49 PM.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    1
    Say hi to Guilliams for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading file into linked lists
    By bcianfrocca in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 03:12 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 10:32 PM
  3. help with linked lists and file i/o
    By Mazer in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 01:59 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. file i/o-structs,linked lists
    By new2c in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2001, 10:31 PM

Tags for this Thread


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