Advertisement
Guest User

Untitled

a guest
Feb 11th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  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) {
  29.             map[i][j] = malloc(2*sizeof(char));
  30.         }
  31.     }
  32.    
  33.     /* Read file into array */
  34.     row = 0, col = 0;
  35.     while (fgets(charbuffer, 3, mapfile)) {
  36.         if (*charbuffer != '\n') {map[row][col] = strdup(charbuffer); col++;}
  37.         else {row++; col = 0;}
  38.     }
  39.     fclose(mapfile);
  40.  
  41.     /* Print array */
  42.     for (row = 0; row < ROWS; row++) {
  43.         for (col = 0; col < COLS; col++) {
  44.             printf("%s", map[row][col]);
  45.         }
  46.         printf("\n");
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement