943,981 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2805
  • C RSS
May 3rd, 2006
0

more linked list printing problems

Expand Post »
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


  1. int loadData(GJCType* menu, char* menuFile, char* submenuFile)
  2. {
  3. /*pointer for malloc*/
  4. CategoryType *newCat, *currentCat, *prevCat;
  5.  
  6.  
  7.  
  8. /*declaration of variables*/
  9. int CountNoLines = 0;
  10. char temp[LINE_LENGTH];
  11.  
  12. /* variables for tokenizer for menu file*/
  13. char *catID;
  14. char *catType;
  15. char *catName;
  16. char *catDescription;
  17. char *delim;
  18.  
  19. /* declare file pointers to files*/
  20. FILE *f1;
  21. FILE *f2;
  22.  
  23.  
  24. /* opening the menu and submenu for reading*/
  25. f1 = fopen(menuFile, "r");
  26. f2 = fopen(submenuFile, "r");
  27.  
  28. /* check to see if the filuse exists*/
  29. if(f1 ==NULL || f2 == NULL)
  30. {
  31. /* check whether both files have existed or typed in correctly*/
  32. if(f1 == NULL && f2 == NULL)
  33. {
  34.  
  35. printf("Both files do not exist %s %s\n",menuFile,submenuFile);
  36. }
  37. /* check for each file existance*/
  38. else if (f1 == NULL)
  39. {
  40. printf("%s does not exist\n\n",menuFile);
  41. }
  42. /* check for each file existance*/
  43. else if(f2 == NULL)
  44. {
  45. printf("%s does not exist\n\n", submenuFile);
  46.  
  47. }
  48. printf("EXITING PROGRAM ERROR!\n\n");
  49. return ERRORCODE; /* cannot proceed*/
  50. }
  51.  
  52. /* counts how many fields there are in the file*/
  53. while(fgets(temp, LINE_LENGTH, f1)!=NULL)
  54. {
  55. newCat = NULL;
  56. prevCat = NULL;
  57. currentCat = NULL;
  58.  
  59. newCat = malloc(sizeof(CategoryType));
  60.  
  61. if(prevCat == NULL)
  62. {
  63. menu->headCategory = newCat;
  64. }
  65. else
  66. {
  67.  
  68.  
  69.  
  70. prevCat->nextCategory = newCat;
  71.  
  72.  
  73.  
  74. /* stores the catID but also checks if there
  75. is a duplicate*/
  76.  
  77. catID = strtok(temp, "|");
  78.  
  79. /* checking whether it tokenizez*/
  80. if(catID == NULL)
  81. {
  82. printf("CatID missing\n");
  83. }
  84.  
  85. /*checks for a duplication of the id field*/
  86. if(!checkDuplicationID(menu, catID))
  87. {
  88. return ERRORCODE;
  89. }
  90. /* gets the second field and tokenizez it*/
  91. catType = strtok(NULL, "|");
  92.  
  93. /* checking whether it tokenizez*/
  94. if(catType == NULL)
  95. {
  96. printf("CatType missing\n");
  97. }
  98.  
  99. catName = strtok(NULL, "|");
  100.  
  101. /* checking whether it tokenizez*/
  102. if(catName == NULL)
  103. {
  104. printf("CatName missing\n");
  105. }
  106. catDescription = strtok(NULL, "\0");
  107.  
  108. /* checking whether it tokenizez*/
  109. if(catDescription == NULL)
  110. {
  111. printf("CatDescription missing\n");
  112. }
  113.  
  114. /* checks the string length of the field*/
  115. if((strlen(catID) >ID_LEN) ||
  116. (strlen(catType) >MAX_NAME_LEN) ||
  117. (strlen(catDescription) >MAX_DESC_LEN))
  118.  
  119. {
  120.  
  121. printf("Wrong data format\n\n");
  122. return ERRORCODE;
  123.  
  124. }
  125.  
  126. strcpy(newCat->categoryID, catID);
  127. newCat->drinkType = catType[0];
  128. strcpy(newCat->categoryName, catName);
  129. strcpy(newCat->categoryDescription, catDescription);
  130.  
  131.  
  132.  
  133. newCat -> nextCategory = NULL;
  134. newCat->headItem = NULL;
  135. newCat->numItems = 0;
  136. prevCat = newCat;
  137.  
  138. currentCat = menu->headCategory;
  139.  
  140. }/* end of if else used to tokenize and copy variables
  141. from and into structs*/
  142.  
  143. while(currentCat!=NULL)
  144. {
  145. printf("%s, %c, %s, %s", currentCat->categoryID,
  146. currentCat->drinkType, currentCat->categoryName,
  147. currentCat->categoryDescription);
  148.  
  149. currentCat = currentCat->nextCategory;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. } /* end of while loop*/
  156.  
  157.  
  158.  
  159. /* close both files after reading*/
  160. if(fclose(f1)!=0 || fclose(f2)!=0)
  161. {
  162. fprintf(stderr, "Error in closing files\n");
  163. }
  164.  
  165.  
  166. return EXIT_SUCCESS;
  167. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
musicmancanora4 is offline Offline
34 posts
since Mar 2006
May 3rd, 2006
0

Re: more linked list printing problems

Quote ...
while(fgets(temp, LINE_LENGTH, f1)!=NULL)
{
newCat = NULL;
prevCat = NULL;
currentCat = NULL;

newCat = malloc(sizeof(CategoryType));

if(prevCat == NULL)
{
menu->headCategory = newCat;
}
else
{
In the above snippet prevCat is initialized to NULL each time the loop starts to the conditional of the if statement will always be true and the else statement will never be reached.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: struct linked list problem
Next Thread in C Forum Timeline: Access Violation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC