life.c
changeset 8 96d16dfe787a
parent 0 af501b0c1716
child 9 c3fecc82ade6
equal deleted inserted replaced
7:a1aa30f0f904 8:96d16dfe787a
    23     FILE *inp = NULL;
    23     FILE *inp = NULL;
    24 
    24 
    25     while (inp == NULL) {
    25     while (inp == NULL) {
    26         printf ("Please enter the file name>> ");
    26         printf ("Please enter the file name>> ");
    27         if (scanf ("%s", file) < 1)
    27         if (scanf ("%s", file) < 1)
    28 			return -1;
    28             return -1;
    29 
    29 
    30         inp = fopen (file, "r");
    30         inp = fopen (file, "r");
    31     }
    31     }
    32 
    32 
    33     for (i = 0; i < MAX; i++) {
    33     for (i = 0; i < MAX; i++) {
    36             if (feof (inp)) {
    36             if (feof (inp)) {
    37                 printf ("EOF detected\n");
    37                 printf ("EOF detected\n");
    38                 return fclose (inp);
    38                 return fclose (inp);
    39             }
    39             }
    40 
    40 
    41             if ( (fscanf (inp, "%c ", &org)) < 1 )
    41             if ((fscanf (inp, "%c ", &org)) < 1)
    42 				return -1;
    42                 return -1;
    43 
    43 
    44             arena[i][j] = org;
    44             arena[i][j] = org;
    45             printf ("%c ", arena[i][j]);
    45             printf ("%c ", arena[i][j]);
    46         }
    46         }
    47         printf ("\n");
    47         printf ("\n");
    55     int i, j;
    55     int i, j;
    56 
    56 
    57     for (i = 0; i < MAX; i++) {
    57     for (i = 0; i < MAX; i++) {
    58         if ((arena[i] = malloc (MAX + 1)) == NULL)
    58         if ((arena[i] = malloc (MAX + 1)) == NULL)
    59             return -1;
    59             return -1;
    60 		for (j=0;j<MAX;j++)
    60         for (j = 0; j < MAX; j++)
    61 			arena[i][j] = '.';
    61             arena[i][j] = '.';
    62     }
    62     }
    63 
    63 
    64     for (i = 0; i < MAX; i++) {
    64     for (i = 0; i < MAX; i++) {
    65         arena[0][i] = '-';
    65         arena[0][i] = '-';
    66         arena[MAX - 1][i] = '-';
    66         arena[MAX - 1][i] = '-';
   105 
   105 
   106 int main (int argc, char **argv)
   106 int main (int argc, char **argv)
   107 {
   107 {
   108     char *arena[MAX];
   108     char *arena[MAX];
   109 
   109 
   110 	if (argc > 2)
   110     if (argc > 2)
   111 		printf("Usage: %s\n", argv[0]);
   111         printf ("Usage: %s\n", argv[0]);
   112 
   112 
   113     if (init_life (arena) == 0)
   113     if (init_life (arena) == 0)
   114     	show_life (arena);
   114         show_life (arena);
   115     free_life (arena);
   115     free_life (arena);
   116 
   116 
   117     return 0;
   117     return EXIT_SUCCESS;
   118 }
   118 }