944,058 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5740
  • C RSS
May 2nd, 2006
0

struct linked list problem

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
musicmancanora4 is offline Offline
34 posts
since Mar 2006
May 2nd, 2006
0

Re: struct linked list problem

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.
Reputation Points: 12
Solved Threads: 0
Light Poster
dude543 is offline Offline
26 posts
since Apr 2006
May 3rd, 2006
0

Re: struct linked list problem

thanks got it...
Reputation Points: 10
Solved Threads: 0
Light Poster
musicmancanora4 is offline Offline
34 posts
since Mar 2006

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: need help in animal game guessing
Next Thread in C Forum Timeline: more linked list printing problems





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


Follow us on Twitter


© 2011 DaniWeb® LLC