-
Weird problem
Here is my input for this code.
1
11111111111111111111
11111111111111111111
1111S000G11111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
The problem.
When checking for char 'S' the array is showing it is finding S at element [2][7] this is obviously incorrect.
Code:
FILE *infile; // file
// If statement for opening file and error for fail.
if((infile = fopen(file, "r")) == NULL)
{
printf("Error Opening File. \n");
exit(1);
}
fscanf(infile, "%d", &num_of_mazes);
char **maze;
maze = (char **) malloc(20 * sizeof(char *));
if(maze == NULL)
{
printf("ERROR cannot be allocated");
}
for(i = 0; i < 20; i++)
{
maze[i] = (char *) malloc(20 * sizeof(char));
}
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
fscanf(infile, "%c", &maze[k][j]);
}
}
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
printf("%c", maze[k][j]);
if(maze[k][j] == 'S')
{
printf("%d %d", k, j);
}
}
}
Im not sure if I didn't allocate right.. or if my scanf is getting bugged. Anyone have any ideas?
-
You shouldn't cast malloc().
Code:
fscanf(infile, "%c", &maze[k][j]);
Try switching the [k] and [j], maybe?
Your memory looks allocated properly (but it isn't freed).
-
Nope switching it isnt the problem, products output like so.
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
111111111111111S7 21111
11111111111111011111
11111111111110111111
11111111111101111111
11111111111G11111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
11111111111111111111
-
is always 1, you don't need it in there.
Try this:
Code:
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
printf("%c", maze[k][j]);
if(maze[k][j] == 'S')
{
printf("%d %d", k, j);
}
}
printf("\n");
}
-
Code:
fscanf(infile, "%c", &maze[k][j]);
will read the lineterminater '\n' as well. This messes up your line and column indexes.
Kurt
-
using this code:
Code:
char **maze;
maze = (char **) malloc(20 * sizeof(char *));
if(maze == NULL)
{
printf("ERROR cannot be allocated");
}
for(i = 0; i < 20; i++)
{
maze[i] = (char *) malloc(20);
}
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
fscanf(infile, "%c", &maze[k][j]);
}
}
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
printf("%c", maze[k][j]);
if(maze[k][j] == 'S')
{ printf("%d %d", k, j);
}
} printf("\n");
}
I get this output.
1111111111111111111
1
111111111111111111
11
1111S2 7000G1111111
111
1111111111111111
1111
111111111111111
11111
11111111111111
111111
1111111111111
1111111
111111111111
11111111
11111111111
111111111
1111111111
1111111111
111111111
11111111111
11111111
111111111111
1111111
1111111111111
111111
11111111111111
11111
111111111111111
1111
1111111111111111
111
11111111111111111
11
111111111111111111
1
1111111111111111111
as you can see, its still recognizing the 'S' as element 7.. what the heck.
-
Code:
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
fscanf(infile, "%c", &maze[k][j]);
}
}
The problem is there's a newline character at the end of the line, so you are reading that also. So you could add an extra fscanf() call after the first loop:
Code:
for(k = 0; k < 20; k++)
{
for(j = 0; j < 20; j++)
{
fscanf(infile, "%c", &maze[k][j]);
}
fscanf(infile, "%*c");
}
Or you could read a line with fgets(), then scan with sscanf(), and you wouldn't need the extra call to sscanf().
-
-
You can just put a \n in your scanfs
Code:
fscanf(infile, "%d\n", &num_of_mazes);
fscanf(infile, "%c\n", &maze[k][j]);