I am writing a program in C to solve a maze game. The input maze file will be read from stdin. I have written below program which read the maze from stdin and prints no. of rows and columns. But once I read my input file completely how can I access it again so that I can perform the next steps?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUFFERSIZE (1000)
struct maze {
char ** map;
int startx, starty;
int numrows;
int initdir;
};
void ReadMaze(char * filename, struct maze * maze);
int main(int argc, char *argv[]) {
struct maze maze;
ReadMaze(argv[1], &maze);
return EXIT_SUCCESS;
}
/* Creates a maze from a file */
void ReadMaze(char * filename, struct maze * maze) {
char buffer[BUFFERSIZE];
char mazeValue [BUFFERSIZE][BUFFERSIZE];
char ** map;
int rows = 0, foundentrance = 0, foundexit = 0;
int columns = 0;
/* Determine number of rows in maze */
while ( fgets(buffer, BUFFERSIZE, stdin) ){
++rows;
puts(buffer);
columns = strlen(buffer);
}
printf("No of rows: %d\n", rows);
printf("No of columns: %d\n", columns);
if ( !(map = malloc(rows * sizeof *map)) ) {
fputs("Couldn't allocate memory for map\n", stderr);
exit(EXIT_FAILURE);
}
}