Se o mapa nao tiver preenchimento, a funcao linha inferior e a coluna da direita nao sao impressas em tela. Como corrigir?
Ex:
Mapa.txt
|---------|
| |
| |
| |
|---------|
Apos execucao:
|---------|
|
|
|
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Map{
char** board;
int lines;
int columns;
} tMap;
void readMapFromFile(tMap* m);
void allocateMemoryForMap(tMap* m);
void releaseMemoryFromMap(tMap* m);
void printMap(tMap* m);
int main()
{
tMap m;
readMapFromFile(&m);
printMap(&m);
releaseMemoryFromMap(&m);
}
void readMapFromFile(tMap* m){
FILE* fMap;
fMap = fopen("map.txt","r");
//try to open file in reading mode
if (fMap == NULL){
printf("Error to open file.");
exit(1);
}
//read number of lines and columns of the map
fscanf(fMap, "%d %d", &(m->lines), &(m->columns));
allocateMemoryForMap(m);
//load map to the memory
for (int i = 0; i < m->lines; i++){
fscanf(fMap, "%s", m->board[i]);
}
fclose(fMap);
}
void allocateMemoryForMap(tMap* m){
//allocate memory for map lines
m->board = malloc(sizeof(char*) * m->lines);
//for each line allocate memory for map columns
for (int i = 0; i < m->lines; i++){
m->board[i] = malloc(sizeof(char) * (m->columns + i));
}
}
void releaseMemoryFromMap(tMap* m){
for(int i = 0; i < m->lines; i++) {
free(m->board[i]);
}
free(m->board);
}
void printMap(tMap* m){
for (int i = 0; i < m->lines; i++){
printf("%s\n", m->board[i]);
}
/*for (int i = 0; i < m->lines; i++){
for (int j = 0; j < m->columns; j++){
printf("%c", m->board[i][j]);
}
printf("\n");
}*/
}