0

I am solving a maze problem in C program using input redirection. I am not much familiar with C language and need some input in below code wherein I am not able to read the file properly. Can you please point out the mistake so that I can proceed ahead. Thanks.

#include <stdio.h>
#include <stdlib.h>

#define MAZESIZE (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;

    if ( argc < 2 ) {
        puts("Please specify the filename of your maze");
        return EXIT_FAILURE;
    }
    else if ( argc > 2 ) {
        puts("Too many input files");
        return EXIT_FAILURE;
    }
    ReadMaze(argv[1], &maze);

    return EXIT_SUCCESS;
}


void ReadMaze(char * filename, struct maze * maze) {
    char buffer[MAZESIZE];
    char ** map;
    int n = 0, foundentrance = 0, foundexit = 0;

    while ( fgets(buffer, MAZESIZE, stdin) )
    {
       printf("No of rows inside: %d", n); 
       ++n;
    }

    printf("No of rows: %d", n);

}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 2
    You are currently reading from `stdin` (the keyboard). You need to `fopen` the file to be able to read from it. There are tons of tutorials online on how to do that. – Klas Lindbäck May 17 '16 at 08:51
  • I have been asked not to use fopen. Need to do it using input redirection. – user6344678 May 17 '16 at 08:53
  • Possible duplicate of [Rerouting stdin and stdout from C](http://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c) – Garf365 May 17 '16 at 08:55
  • 1
    you should use `freopen` (http://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c). If you can't, it's not with C you can redirect standard input... if you use Linux, you can launch your program like that : `prog < maze.txt`... – Garf365 May 17 '16 at 08:56
  • You should add `\n` at the end of your `printf`s. Ending with a newline will make output prettier. In most operating systems you use `<` for redirection of input, so you could run you program like this: `ReadMaze < maze.txt` (Replace `ReadMaze` with the actual name of your program and `maze.txt` with the actual name of your maze file.) – Klas Lindbäck May 17 '16 at 08:58
  • 4
    And since you will be using redirection you won't need `argc`/`argv`. – Klas Lindbäck May 17 '16 at 09:00
  • Thanks Klas and everyone. As I am using input redirection, I have removed argc and ran the program as suggested by Klas, its working fine now. – user6344678 May 17 '16 at 09:16

0 Answers0