Question
I'm working on an assignment and have hit a dead-end. Please look over my code below. Theres an explination below the code. For this assignment
I'm working on an assignment and have hit a dead-end. Please look over my code below. Theres an explination below the code.
For this assignment we have to create 10 diffrent rooms, and randomly connect 7 of them together to impliment a mini maze game later (start in one room, try to get to the final room basically). I have the randomization parts finished and working, but for each room I have to output its name, which rooms its connected too, and whether its a start room, mid room, or the ending room into its own file. Then those files all get placed into one directory named "luthco.rooms.PROCESSID". Each room file is simply named after the room's name.
I can't figure out what it going wrong with my code. I keep getting a segmentation fault error.
I know I must be trying to access memory that I am not allowed to access, but I cant figure out where its happening. But I definatly know its happening within this snippet of code.
Thank you!
188 int pid=getpid(); 189 190 char dirName [NAME MAX+ /allocates space for the directory name 191 192 char* prefix = "luthco.rooms.". //directory name into a temperary char //Gets Process ID //printf("Creating dirName) //printf("Creating prefix"); printf("Creating snprintf") snprint f (dirNane, "%s%d", pid); 194 195 196 197 1 98 199 200 201 202 203 204 205 206 207 208 209 210 NAME-MAXt1, //combines the temperary char and process id onto end, intodirNane prefix, //printf("stats(dirName blah)") if (stat (dirName, 0755)= if (mkdir(dirane, 0755) for(x ; x active char LibPath [PATH_MAX+1]: ALLocates space for name of file path (LibPath) snprintf(libPath, PATH MAX + 1, %s/8s. txt", dirName, Rooms [x]->name); /places the room name onto the end of diniame and pu ts into Liiath, FILE *fub = fopen (libPath , "ra+"); //creates a new file in the directory ubPath fprintf(flb, "ROOM NAME: %s ", Rooms [x]->name); //Places things into the file int k; = //printf ("Room is Active, creating ibPath"); //printf("libPath Made, snprintf); /printff"snprintf made, creating File") //printf("Printing to file) printf" printed name to file, printing connections) k++) Rooms [x1-> outboundConnectionslkl-name); { Rooms [x]->nunOutboundConnections; %s (k 0; k 212 213 214 215 216 217 218 for = type); //Places more things into the tile //printf("Printed room type to file") fclose(flib); //Closes the file else t 220 221 perror("mkdir:"): //If the above fails then it will error 223 188 int pid=getpid(); 189 190 char dirName [NAME MAX+ /allocates space for the directory name 191 192 char* prefix = "luthco.rooms.". //directory name into a temperary char //Gets Process ID //printf("Creating dirName) //printf("Creating prefix"); printf("Creating snprintf") snprint f (dirNane, "%s%d", pid); 194 195 196 197 1 98 199 200 201 202 203 204 205 206 207 208 209 210 NAME-MAXt1, //combines the temperary char and process id onto end, intodirNane prefix, //printf("stats(dirName blah)") if (stat (dirName, 0755)= if (mkdir(dirane, 0755) for(x ; x active char LibPath [PATH_MAX+1]: ALLocates space for name of file path (LibPath) snprintf(libPath, PATH MAX + 1, %s/8s. txt", dirName, Rooms [x]->name); /places the room name onto the end of diniame and pu ts into Liiath, FILE *fub = fopen (libPath , "ra+"); //creates a new file in the directory ubPath fprintf(flb, "ROOM NAME: %s ", Rooms [x]->name); //Places things into the file int k; = //printf ("Room is Active, creating ibPath"); //printf("libPath Made, snprintf); /printff"snprintf made, creating File") //printf("Printing to file) printf" printed name to file, printing connections) k++) Rooms [x1-> outboundConnectionslkl-name); { Rooms [x]->nunOutboundConnections; %s (k 0; k 212 213 214 215 216 217 218 for = type); //Places more things into the tile //printf("Printed room type to file") fclose(flib); //Closes the file else t 220 221 perror("mkdir:"): //If the above fails then it will error 223Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started