Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 11th, 2012  |  syntax: C  |  size: 1.04 KB  |  views: 74  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }