Files with Dual Data Structure

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Files with Dual Data Structure

 
0
  #1
Sep 21st, 2009
Hello. I'm not getting the desired output for my program. The problem is specifically in the file. I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error. Here's my code. Please help
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. #define max 10
  7. #define gmax 100
  8.  
  9. typedef struct cell{
  10. char name[24];
  11. int rank;
  12. struct cell *next;
  13. }node, *nodeptr;
  14.  
  15. typedef struct{
  16. nodeptr header[max];
  17. nodeptr rankladder[gmax];
  18. int nextrung;
  19. }DDS;
  20.  
  21. void initialize(DDS *D);
  22. int hash(char n[]);
  23. void insert(DDS *D, char n[]);
  24. void challenge(DDS *D, char n[]);
  25. void display_storage(DDS D);
  26. void display_rank(DDS D);
  27. void save(DDS D);
  28. void load(DDS *D);
  29.  
  30. void initialize(DDS *D)
  31. {
  32. int ctr;
  33.  
  34. for(ctr = 0; ctr < max; ctr++)
  35. D->header[ctr] = NULL;
  36.  
  37. D->nextrung = 0;
  38. }
  39.  
  40. int hash(char n[])
  41. {
  42. int ctr, value = 0;
  43.  
  44. for(ctr = 0; ctr < strlen(n); ctr++)
  45. value += (int)n[ctr];
  46.  
  47. return value % max;
  48. }
  49.  
  50. void insert(DDS *D, char n[])
  51. {
  52. nodeptr temp;
  53. int h;
  54.  
  55. temp = (nodeptr) malloc(sizeof(node));
  56. h = hash(n);
  57.  
  58. strcpy(temp->name, n);
  59.  
  60. temp->next = D->header[h];
  61. D->header[h] = temp;
  62. D->rankladder[++(D->nextrung)] = temp;
  63. temp->rank = D->nextrung;
  64. }
  65. void challenge(DDS *D, char n[])
  66. {
  67. nodeptr p;
  68. int h, Challenger, Challengee, flag = 0;
  69.  
  70. h = hash(n);
  71. randomize();
  72.  
  73. for(p = D->header[h]; p != NULL && flag == 0;)
  74. {
  75. if(strcmp(p->name, n) == 0)
  76. {
  77. if(p->rank != 1)
  78. {
  79. printf("%s vs %s!\n", D->rankladder[p->rank - 1]->name, p->name);
  80. Challenger = rand();
  81. Challengee = rand();
  82.  
  83. if(Challenger < Challengee)
  84. {
  85. D->rankladder[p->rank - 1]->rank++;
  86. D->rankladder[p->rank] = D->rankladder[p->rank - 1];
  87. p->rank--;
  88. D->rankladder[p->rank] = p;
  89. printf("%s won!\n", p->name);
  90. }
  91. else
  92. printf("%s lost\n", p->name);
  93. }
  94. else
  95. printf("%s is the best there is, there are no challengers\n", n);
  96. flag = 1;
  97. }
  98. else
  99. p = p->next;
  100. }
  101.  
  102.  
  103. if(p == NULL)
  104. printf("%s does not exist\n", n);
  105.  
  106. getch();
  107.  
  108. }
  109.  
  110. void display_storage(DDS D)
  111. {
  112. int ctr;
  113. nodeptr p;
  114.  
  115. for(ctr = 0; ctr < max; ctr++)
  116. {
  117. printf("%d ", ctr);
  118. for(p = D.header[ctr]; p != NULL; p = p->next)
  119. printf("%s ", p->name);
  120. printf("\n");
  121. }
  122.  
  123. getch();
  124. }
  125. void display_rank(DDS D)
  126. {
  127. int ctr;
  128.  
  129. printf("Ranking: \n");
  130. for(ctr = 1; ctr <= D.nextrung; ctr++)
  131. printf("%d. %s\n", D.rankladder[ctr]->rank, D.rankladder[ctr]->name);
  132.  
  133. getch();
  134. }
  135. void save(DDS D)
  136. {
  137. FILE *fp;
  138.  
  139. if((fp = fopen("DDSPrac.dat", "wb")) == 0)
  140. {
  141. printf("Cannot write on file\n");
  142. exit(1);
  143. }
  144.  
  145. fwrite(&D, sizeof(DDS), 1, fp);
  146.  
  147. printf("File Saved\n");
  148. fclose(fp);
  149.  
  150. getch();
  151. }
  152.  
  153. void load(DDS *D)
  154. {
  155. FILE *fp;
  156. int ctr, ctr2 = 0;
  157. nodeptr *p;
  158.  
  159. if((fp = fopen("DDSPrac.dat", "rb")) == 0)
  160. {
  161. printf("Cannot open file\n");
  162. exit(1);
  163. }
  164. /*for(ctr = 0; ctr < max; ctr++)
  165. {
  166. p = &D->header[ctr];
  167. do
  168. {
  169. fread(p, sizeof(nodeptr), 1, fp);
  170. printf("%s\n", (*p)->name);
  171. }while(*p != NULL);
  172. }
  173. do
  174. {
  175. p = &D->rankladder[ctr];
  176. }while((fread(p, sizeof(nodeptr), 1, fp)) == 1);
  177. */
  178. fread(D, sizeof(DDS), 1, fp);
  179. printf("File Loaded\n");
  180.  
  181. fclose(fp);
  182.  
  183. getch();
  184.  
  185. }
  186. void main()
  187. {
  188. DDS D;
  189. char n[24], check;
  190. int h, exit = 0;
  191.  
  192. initialize(&D);
  193.  
  194. do
  195. {
  196. printf("What do you want to do? \n");
  197. printf("Insert(I)\n");
  198. printf("Challenge(C)\n");
  199. printf("Display Storage(D)\n");
  200. printf("Display Rank(R)\n");
  201. printf("Save(S)\n");
  202. printf("Load(L)\n");
  203. printf("Quit(Q)\n");
  204. check = getch();
  205.  
  206.  
  207. if(check == 'I' || check == 'i')
  208. {
  209. printf("Input a name: ");
  210. flushall();
  211. gets(n);
  212.  
  213. insert(&D, n);
  214. }
  215. else if(check == 'D' || check == 'd')
  216. display_storage(D);
  217. else if(check == 'C' || check == 'c')
  218. {
  219. printf("Input the challenger's name: ");
  220. flushall();
  221. gets(n);
  222.  
  223. challenge(&D, n);
  224. }
  225. else if(check == 'R' || check == 'r')
  226. display_rank(D);
  227. else if(check == 'S' || check == 's')
  228. save(D);
  229. else if(check == 'L' || check == 'l')
  230. load(&D);
  231. else if(check == 'Q' || check == 'q')
  232. exit = 1;
  233. else
  234. printf("Error\n");
  235. clrscr();
  236. }while(exit == 0);
  237.  
  238. }
Last edited by Whilliam; Sep 21st, 2009 at 12:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Files with Dual Data Structure

 
0
  #2
Sep 21st, 2009
I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error.
Your data structure is probably getting corrupted during the load. Maybe you are not terminating one of the lists with NULL, or not handling NULL cells as a special case and trying to dereference them. The best way to troubleshoot is to find a small case that fails and step through it with a debugger while watching your memory for changes.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC