SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | ||
5 | void mapopen(); | |
6 | ||
7 | int main() { | |
8 | mapopen(); | |
9 | } | |
10 | ||
11 | void mapopen() { | |
12 | FILE *mapfile = fopen("map", "r"); | |
13 | char charbuffer[3], row, col, ROWS, COLS, i, j; | |
14 | ||
15 | /* Count number of rows and columns in map */ | |
16 | row = 0, col = 0; | |
17 | while (fgets(charbuffer, 3, mapfile)) { | |
18 | if (*charbuffer != '\n') {col++;} | |
19 | else {row++; COLS = col; col = 0;} | |
20 | } | |
21 | ROWS = row; | |
22 | printf("%d rows %d cols", ROWS, COLS); | |
23 | ||
24 | /* Create map */ | |
25 | char ***map = malloc(ROWS * sizeof(char**)); | |
26 | for (i = 0; i < ROWS; i++) { | |
27 | map[i] = malloc(COLS * sizeof(char*)); | |
28 | - | for (j = 0; i < COLS; ++j) { |
28 | + | for (j = 0; j < COLS; j++) {map[i][j] = malloc(3 * sizeof(char));} |
29 | - | map[i][j] = malloc(2*sizeof(char)); |
29 | + | |
30 | ||
31 | /* Read file into array */ | |
32 | - | |
32 | + | |
33 | while (fgets(charbuffer, 3, mapfile)) { | |
34 | if (*charbuffer != '\n') {map[row][col] = strdup(charbuffer); col++;} | |
35 | else {row++; col = 0;} | |
36 | } | |
37 | fclose(mapfile); | |
38 | ||
39 | /* Print array */ | |
40 | for (row = 0; row < ROWS; row++) { | |
41 | for (col = 0; col < COLS; col++) { | |
42 | printf("%s", map[row][col]); | |
43 | } | |
44 | printf("\n"); | |
45 | } | |
46 | } |