Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created January 26, 2015 20:19
Show Gist options
  • Select an option

  • Save mauricioaniche/83a9b17ad588b37d9431 to your computer and use it in GitHub Desktop.

Select an option

Save mauricioaniche/83a9b17ad588b37d9431 to your computer and use it in GitHub Desktop.

Revisions

  1. mauricioaniche created this gist Jan 26, 2015.
    52 changes: 52 additions & 0 deletions fogefoge.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include "fogefoge.h"

    char** mapa;
    int linhas;
    int colunas;

    void lemapa() {
    FILE* f;
    f = fopen("mapa.txt", "r");
    if(f == 0) {
    printf("Erro na leitura do mapa");
    exit(1);
    }

    fscanf(f, "%d %d", &linhas, &colunas);
    alocamapa();

    for(int i = 0; i < 5; i++) {
    fscanf(f, "%s", mapa[i]);
    }

    fclose(f);
    }

    void alocamapa() {
    mapa = malloc(sizeof(char*) * linhas);

    for(int i = 0; i < linhas; i++) {
    mapa[i] = malloc(sizeof(char) * colunas + 1);
    }
    }

    void liberamapa() {
    for(int i = 0; i < linhas; i++) {
    free(mapa[i]);
    }

    free(mapa);
    }

    int main() {

    lemapa();

    for(int i = 0; i < linhas; i++) {
    printf("%s\n", mapa[i]);
    }

    liberamapa();
    }
    3 changes: 3 additions & 0 deletions fogefoge.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    void alocamapa();
    void lemapa();
    void liberamapa();
    6 changes: 6 additions & 0 deletions mapa.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    5 10
    |--------|
    |...|..-.|
    |..-|.@..|
    |......-.|
    |--------|