OK. Here is the complete function. Things are fine until I try to fill my array with the patches from the file input. I will repost that here:
Code:
rect
7 9
+-------+
| |
| |
| |
| |
| |
+-------+
ex
5 5
* *
* *
*
* *
* *
dollar
1 1
$
the rect line is loaded into the struct just fine, as are the dimensions(7 9). At line 3, I am using the dimensions as my height and width to fill my two dimensional array, which is in my structure.
The problem is in my for loop logic I am sure, but I cannot seem to locate it:
Code:
void get_patches(patch *p){
char line[100];
int count = 0;
while(fgets(line, sizeof(line), patches) != NULL) {
//name
strcpy(p->name, line);
//height and width
fscanf(patches,"%d %d", &p->height, &p->width);
//get the patch into an array
int i;
int j;
for (i = 0; i < p->height; ++i){
fgets(line, sizeof(line), patches);
for (j = 0; j < p->width; ++j){
p->patch[i][j] = line[j];
//debug
printf("%c", p->patch[i][j]);
}
}
system("PAUSE");
//place the structure into an array
mystructures[count] = *p;
count++;
}
}
It does not seem to be breaking the for loop in the correct spot throwing the rest of the program in disarray and eventually crashing. Here is some sample output from before it crashes:
Code:
File List:
patches1.txt
patches2.txt
patches3.txt
commands1.txt
commands2.txt
commands3.txt
Input a patches filename: patches1.txt
ct
ñ+-------+| || || || || |Press any key to continue . . .
ex
----+5 5
---+* *
-+ * *
-+ *
-+ * *
-+* *
-+Press any key to continue . . .
Press any key to continue . . .
$Press any key to continue . . .
As you can see, the for loop is grabbing and placing the next name and dimensions, whereas it's not supposed to. It is supposed to break at the end of the patch so the main control loop can start in the correct position. Can anyone see my mistake?