| | |
more linked list printing problems
![]() |
•
•
Join Date: Mar 2006
Posts: 28
Reputation:
Solved Threads: 0
Sorry for posting again guys but i have fixed up teh string copies to copy the data into each struct variable. But still when i traverse through the linked list it does not print out anything after it reads the file. For some reason it may not be copying at all or something any idea's?
i think its because currentCat variables is pointing to NULL rather then a node but im unsure how
to fix it cuz it wont print any of the data which is being loaded
i think its because currentCat variables is pointing to NULL rather then a node but im unsure how
to fix it cuz it wont print any of the data which is being loaded
C Syntax (Toggle Plain Text)
int loadData(GJCType* menu, char* menuFile, char* submenuFile) { /*pointer for malloc*/ CategoryType *newCat, *currentCat, *prevCat; /*declaration of variables*/ int CountNoLines = 0; char temp[LINE_LENGTH]; /* variables for tokenizer for menu file*/ char *catID; char *catType; char *catName; char *catDescription; char *delim; /* declare file pointers to files*/ FILE *f1; FILE *f2; /* opening the menu and submenu for reading*/ f1 = fopen(menuFile, "r"); f2 = fopen(submenuFile, "r"); /* check to see if the filuse exists*/ if(f1 ==NULL || f2 == NULL) { /* check whether both files have existed or typed in correctly*/ if(f1 == NULL && f2 == NULL) { printf("Both files do not exist %s %s\n",menuFile,submenuFile); } /* check for each file existance*/ else if (f1 == NULL) { printf("%s does not exist\n\n",menuFile); } /* check for each file existance*/ else if(f2 == NULL) { printf("%s does not exist\n\n", submenuFile); } printf("EXITING PROGRAM ERROR!\n\n"); return ERRORCODE; /* cannot proceed*/ } /* counts how many fields there are in the file*/ while(fgets(temp, LINE_LENGTH, f1)!=NULL) { newCat = NULL; prevCat = NULL; currentCat = NULL; newCat = malloc(sizeof(CategoryType)); if(prevCat == NULL) { menu->headCategory = newCat; } else { prevCat->nextCategory = newCat; /* stores the catID but also checks if there is a duplicate*/ catID = strtok(temp, "|"); /* checking whether it tokenizez*/ if(catID == NULL) { printf("CatID missing\n"); } /*checks for a duplication of the id field*/ if(!checkDuplicationID(menu, catID)) { return ERRORCODE; } /* gets the second field and tokenizez it*/ catType = strtok(NULL, "|"); /* checking whether it tokenizez*/ if(catType == NULL) { printf("CatType missing\n"); } catName = strtok(NULL, "|"); /* checking whether it tokenizez*/ if(catName == NULL) { printf("CatName missing\n"); } catDescription = strtok(NULL, "\0"); /* checking whether it tokenizez*/ if(catDescription == NULL) { printf("CatDescription missing\n"); } /* checks the string length of the field*/ if((strlen(catID) >ID_LEN) || (strlen(catType) >MAX_NAME_LEN) || (strlen(catDescription) >MAX_DESC_LEN)) { printf("Wrong data format\n\n"); return ERRORCODE; } strcpy(newCat->categoryID, catID); newCat->drinkType = catType[0]; strcpy(newCat->categoryName, catName); strcpy(newCat->categoryDescription, catDescription); newCat -> nextCategory = NULL; newCat->headItem = NULL; newCat->numItems = 0; prevCat = newCat; currentCat = menu->headCategory; }/* end of if else used to tokenize and copy variables from and into structs*/ while(currentCat!=NULL) { printf("%s, %c, %s, %s", currentCat->categoryID, currentCat->drinkType, currentCat->categoryName, currentCat->categoryDescription); currentCat = currentCat->nextCategory; } } /* end of while loop*/ /* close both files after reading*/ if(fclose(f1)!=0 || fclose(f2)!=0) { fprintf(stderr, "Error in closing files\n"); } return EXIT_SUCCESS; }
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 262
•
•
•
•
while(fgets(temp, LINE_LENGTH, f1)!=NULL)
{
newCat = NULL;
prevCat = NULL;
currentCat = NULL;
newCat = malloc(sizeof(CategoryType));
if(prevCat == NULL)
{
menu->headCategory = newCat;
}
else
{
![]() |
Similar Threads
- help implementing singly linked list (C++)
- linked list problem!!! (C)
- adding and deleting node in a linked list (C++)
- Linked list search problems (Computer Science)
- doubly linked list--- help needed1 (C++)
- printing a linked list (C++)
- Linked List (C++)
Other Threads in the C Forum
- Previous Thread: struct linked list problem
- Next Thread: Access Violation
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






