|
1 /** |
|
2 * $Id$ |
|
3 * $Source$ |
|
4 * |
|
5 */ |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <stdlib.h> |
|
9 |
|
10 #define MAX 12 |
|
11 |
|
12 int read_file (char **); |
|
13 int init_life (char **); |
|
14 void free_life (char **); |
|
15 void show_life (char **); |
|
16 |
|
17 int read_file (char **arena) |
|
18 { |
|
19 int i, j; |
|
20 char org; |
|
21 |
|
22 char file[80]; |
|
23 FILE *inp = NULL; |
|
24 |
|
25 while (inp == NULL) { |
|
26 printf ("Please enter the file name>> "); |
|
27 if (scanf ("%s", file) < 1) |
|
28 return -1; |
|
29 |
|
30 inp = fopen (file, "r"); |
|
31 } |
|
32 |
|
33 for (i = 0; i < MAX; i++) { |
|
34 printf ("Reading Line %3d: ", i); |
|
35 for (j = 0; j < MAX; j++) { |
|
36 if (feof (inp)) { |
|
37 printf ("EOF detected\n"); |
|
38 return fclose (inp); |
|
39 } |
|
40 |
|
41 if ( (fscanf (inp, "%c ", &org)) < 1 ) |
|
42 return -1; |
|
43 |
|
44 arena[i][j] = org; |
|
45 printf ("%c ", arena[i][j]); |
|
46 } |
|
47 printf ("\n"); |
|
48 } |
|
49 |
|
50 return fclose (inp); |
|
51 } |
|
52 |
|
53 int init_life (char **arena) |
|
54 { |
|
55 int i, j; |
|
56 |
|
57 for (i = 0; i < MAX; i++) { |
|
58 if ((arena[i] = malloc (MAX + 1)) == NULL) |
|
59 return -1; |
|
60 for (j=0;j<MAX;j++) |
|
61 arena[i][j] = '.'; |
|
62 } |
|
63 |
|
64 for (i = 0; i < MAX; i++) { |
|
65 arena[0][i] = '-'; |
|
66 arena[MAX - 1][i] = '-'; |
|
67 arena[i][0] = '|'; |
|
68 arena[i][MAX - 1] = '|'; |
|
69 printf ("Initialize: %d\n", i); |
|
70 } |
|
71 |
|
72 if ((read_file (arena) == EOF)) { |
|
73 printf ("\nread_file: ERROR\n"); |
|
74 return -1; |
|
75 } |
|
76 |
|
77 return 0; |
|
78 } |
|
79 |
|
80 void show_life (char **arena) |
|
81 { |
|
82 int i, j; |
|
83 |
|
84 for (i = 0; i < MAX; i++) { |
|
85 for (j = 0; j < MAX; j++) { |
|
86 printf ("%c", arena[i][j]); |
|
87 } |
|
88 printf ("\n"); |
|
89 } |
|
90 } |
|
91 |
|
92 void free_life (char **arena) |
|
93 { |
|
94 int i; |
|
95 |
|
96 if (arena == NULL) |
|
97 return; |
|
98 |
|
99 for (i = 0; i < MAX; i++) { |
|
100 if (arena[i] != NULL) |
|
101 free (arena[i]); |
|
102 } |
|
103 |
|
104 } |
|
105 |
|
106 int main (int argc, char **argv) |
|
107 { |
|
108 char *arena[MAX]; |
|
109 |
|
110 if (argc > 2) |
|
111 printf("Usage: %s\n", argv[0]); |
|
112 |
|
113 if (init_life (arena) == 0) |
|
114 show_life (arena); |
|
115 free_life (arena); |
|
116 |
|
117 return 0; |
|
118 } |