| | |
Files with Dual Data Structure
![]() |
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
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define max 10 #define gmax 100 typedef struct cell{ char name[24]; int rank; struct cell *next; }node, *nodeptr; typedef struct{ nodeptr header[max]; nodeptr rankladder[gmax]; int nextrung; }DDS; void initialize(DDS *D); int hash(char n[]); void insert(DDS *D, char n[]); void challenge(DDS *D, char n[]); void display_storage(DDS D); void display_rank(DDS D); void save(DDS D); void load(DDS *D); void initialize(DDS *D) { int ctr; for(ctr = 0; ctr < max; ctr++) D->header[ctr] = NULL; D->nextrung = 0; } int hash(char n[]) { int ctr, value = 0; for(ctr = 0; ctr < strlen(n); ctr++) value += (int)n[ctr]; return value % max; } void insert(DDS *D, char n[]) { nodeptr temp; int h; temp = (nodeptr) malloc(sizeof(node)); h = hash(n); strcpy(temp->name, n); temp->next = D->header[h]; D->header[h] = temp; D->rankladder[++(D->nextrung)] = temp; temp->rank = D->nextrung; } void challenge(DDS *D, char n[]) { nodeptr p; int h, Challenger, Challengee, flag = 0; h = hash(n); randomize(); for(p = D->header[h]; p != NULL && flag == 0;) { if(strcmp(p->name, n) == 0) { if(p->rank != 1) { printf("%s vs %s!\n", D->rankladder[p->rank - 1]->name, p->name); Challenger = rand(); Challengee = rand(); if(Challenger < Challengee) { D->rankladder[p->rank - 1]->rank++; D->rankladder[p->rank] = D->rankladder[p->rank - 1]; p->rank--; D->rankladder[p->rank] = p; printf("%s won!\n", p->name); } else printf("%s lost\n", p->name); } else printf("%s is the best there is, there are no challengers\n", n); flag = 1; } else p = p->next; } if(p == NULL) printf("%s does not exist\n", n); getch(); } void display_storage(DDS D) { int ctr; nodeptr p; for(ctr = 0; ctr < max; ctr++) { printf("%d ", ctr); for(p = D.header[ctr]; p != NULL; p = p->next) printf("%s ", p->name); printf("\n"); } getch(); } void display_rank(DDS D) { int ctr; printf("Ranking: \n"); for(ctr = 1; ctr <= D.nextrung; ctr++) printf("%d. %s\n", D.rankladder[ctr]->rank, D.rankladder[ctr]->name); getch(); } void save(DDS D) { FILE *fp; if((fp = fopen("DDSPrac.dat", "wb")) == 0) { printf("Cannot write on file\n"); exit(1); } fwrite(&D, sizeof(DDS), 1, fp); printf("File Saved\n"); fclose(fp); getch(); } void load(DDS *D) { FILE *fp; int ctr, ctr2 = 0; nodeptr *p; if((fp = fopen("DDSPrac.dat", "rb")) == 0) { printf("Cannot open file\n"); exit(1); } /*for(ctr = 0; ctr < max; ctr++) { p = &D->header[ctr]; do { fread(p, sizeof(nodeptr), 1, fp); printf("%s\n", (*p)->name); }while(*p != NULL); } do { p = &D->rankladder[ctr]; }while((fread(p, sizeof(nodeptr), 1, fp)) == 1); */ fread(D, sizeof(DDS), 1, fp); printf("File Loaded\n"); fclose(fp); getch(); } void main() { DDS D; char n[24], check; int h, exit = 0; initialize(&D); do { printf("What do you want to do? \n"); printf("Insert(I)\n"); printf("Challenge(C)\n"); printf("Display Storage(D)\n"); printf("Display Rank(R)\n"); printf("Save(S)\n"); printf("Load(L)\n"); printf("Quit(Q)\n"); check = getch(); if(check == 'I' || check == 'i') { printf("Input a name: "); flushall(); gets(n); insert(&D, n); } else if(check == 'D' || check == 'd') display_storage(D); else if(check == 'C' || check == 'c') { printf("Input the challenger's name: "); flushall(); gets(n); challenge(&D, n); } else if(check == 'R' || check == 'r') display_rank(D); else if(check == 'S' || check == 's') save(D); else if(check == 'L' || check == 'l') load(&D); else if(check == 'Q' || check == 'q') exit = 1; else printf("Error\n"); clrscr(); }while(exit == 0); }
Last edited by Whilliam; Sep 21st, 2009 at 12:46 am.
•
•
•
•
I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error.
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Problem with strtok() for data structure creation (C++)
- Data structure + GUI question. (Java)
- a question about heap data structure (C)
- Data Structure Using JAVA (Java)
- How to delete a data structure in Builder 6.0? (C++)
- Discrete Math or Data structure (Computer Science)
Other Threads in the C Forum
- Previous Thread: Strict Binary Tree
- Next Thread: what is wrong with this code snippet - memset() implementation
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windowsapi





