struct linked list problem

Reply

Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

struct linked list problem

 
0
  #1
May 2nd, 2006
Hey guys im reading in a text file with several fields iv tokenized
the '|' so that it only reads the fields categoryId, drinkType, the categoryName, and categoryDescription.

My question is how come when i read the file and copy the reading buffer
"temp" into each struct it does not print out each line in the text file it prints nothing. I traverse through the list and try to print it out?

C0001|H|Espresso Classics|The traditional espresso favourites




  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.  
  56.  
  57. newCat = malloc(sizeof(CategoryType));
  58.  
  59. if(prevCat == NULL)
  60. {
  61. menu->headCategory = newCat;
  62. }
  63. else
  64. {
  65.  
  66.  
  67.  
  68. prevCat->nextCategory = newCat;
  69.  
  70.  
  71.  
  72. /* stores the catID but also checks if there
  73. is a duplicate*/
  74.  
  75. catID = strtok(temp, "|");
  76.  
  77. /* checking whether it tokenizez*/
  78. if(catID == NULL)
  79. {
  80. printf("CatID missing\n");
  81. }
  82.  
  83. /*checks for a duplication of the id field*/
  84. if(!checkDuplicationID(menu, catID))
  85. {
  86. return ERRORCODE;
  87. }
  88. /* gets the second field and tokenizez it*/
  89. catType = strtok(NULL, "|");
  90.  
  91. /* checking whether it tokenizez*/
  92. if(catType == NULL)
  93. {
  94. printf("CatType missing\n");
  95. }
  96.  
  97. catName = strtok(NULL, "|");
  98.  
  99. /* checking whether it tokenizez*/
  100. if(catName == NULL)
  101. {
  102. printf("CatName missing\n");
  103. }
  104. catDescription = strtok(NULL, "\0");
  105.  
  106. /* checking whether it tokenizez*/
  107. if(catDescription == NULL)
  108. {
  109. printf("CatDescription missing\n");
  110. }
  111.  
  112. /* checks the string length of the field*/
  113. if((strlen(catID) >ID_LEN) ||
  114. (strlen(catType) >MAX_NAME_LEN) ||
  115. (strlen(catDescription) >MAX_DESC_LEN))
  116.  
  117. {
  118.  
  119. printf("Wrong data format\n\n");
  120. return ERRORCODE;
  121.  
  122. }
  123.  
  124. strcpy(newCat->categoryID, temp);
  125. newCat->drinkType = temp[0];
  126. strcpy(newCat->categoryName, temp);
  127. strcpy(newCat->categoryDescription, temp);
  128.  
  129.  
  130.  
  131. newCat -> nextCategory = NULL;
  132. newCat->headItem = NULL;
  133. newCat->numItems = 0;
  134. prevCat = newCat;
  135.  
  136. currentCat = menu->headCategory;
  137.  
  138. }/* end of if else used to tokenize and copy variables
  139. from and into structs*/
  140. while(currentCat!=NULL)
  141. {
  142. printf("%s, %c, %s, %s", currentCat->categoryID,
  143. currentCat->drinkType, currentCat->categoryName,
  144. currentCat->categoryDescription);
  145.  
  146. currentCat = currentCat->nextCategory;
  147. }
  148.  
  149.  
  150.  
  151.  
  152. } /* end of while loop*/
  153.  
  154.  
  155.  
  156. /* close both files after reading*/
  157. if(fclose(f1)!=0 || fclose(f2)!=0)
  158. {
  159. fprintf(stderr, "Error in closing files\n");
  160. }
  161.  
  162.  
  163. return EXIT_SUCCESS;
  164. }

  1. #ifndef GJC_H
  2. #define GJC_H
  3.  
  4. /* System-wide header files. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. /* System-wide constants. */
  9. #define ID_LEN 5
  10. #define MIN_NAME_LEN 1
  11. #define MAX_NAME_LEN 25
  12. #define MIN_DESC_LEN 1
  13. #define MAX_DESC_LEN 250
  14. #define NUM_PRICES 3
  15. #define HOT 'H'
  16. #define COLD 'C'
  17. #define ERRORCODE 1
  18. #define VALID 0
  19. #define LINE_LENGTH 500
  20. #define TOKEN_PRODUCT 4
  21.  
  22. typedef struct category* CategoryTypePtr;
  23. typedef struct item* ItemTypePtr;
  24.  
  25. /* Structure definitions. */
  26. typedef struct price
  27. {
  28. unsigned dollars;
  29. unsigned cents;
  30. } PriceType;
  31.  
  32. typedef struct item
  33. {
  34. char itemID[ID_LEN + 1];
  35. char itemName[MAX_NAME_LEN + 1];
  36. PriceType prices[NUM_PRICES];
  37. char itemDescription[MAX_DESC_LEN];
  38. ItemTypePtr nextItem;
  39. } ItemType;
  40.  
  41. typedef struct category
  42. {
  43. char categoryID[ID_LEN + 1];
  44. char categoryName[MAX_NAME_LEN + 1];
  45. char drinkType; /* (H)ot or (C)old. */
  46. char categoryDescription[MAX_DESC_LEN];
  47. CategoryTypePtr nextCategory;
  48. ItemTypePtr headItem;
  49. unsigned numItems;
  50. } CategoryType;
  51.  
  52. typedef struct gjc
  53. {
  54. CategoryTypePtr headCategory;
  55. unsigned numCategories;
  56. } GJCType;
  57.  
  58. int commandLineArguments(int argc, char* argv[]);
  59. int countToken(FILE *fp, char* temp, int tokenPerLine);
  60. #endif
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: struct linked list problem

 
0
  #2
May 2nd, 2006
Your coping the same value (temp) for all the fileds :

  1. strcpy(newCat->categoryID, temp);
  2. newCat->drinkType = temp[0];
  3. strcpy(newCat->categoryName, temp);
  4. strcpy(newCat->categoryDescription, temp);

You should use the pointers you saved like for example :

  1. strcpy(newCat->categoryDescription, catDescription);



Another thing :
Automatic variables inside a fucntion are initailzed to garabge.
  1. CategoryType *newCat, *currentCat, *prevCat;
You forgot to initalize prevCat before you test it
against a NULL.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

Re: struct linked list problem

 
0
  #3
May 3rd, 2006
thanks got it...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC