I was making a small program that loads a simple map from a text file and places it on a 5x5 grid. In the text file, 0 represents grass and 1 represents dirt.
I have a function in my map.h file that takes characters from a txt file and places them into a 5x5 array like so:
Code:
#define T_SIZE 5
char t_type[T_SIZE][T_SIZE];
//Load Terrain
bool LoadTerrain(char *t_file) {
//Pointer to the file to be loaded
FILE *fp;
//Open the file
if((fp=fopen(t_file,"r"))==NULL) return false;
//Loop through each tile
for(int a=0;a<T_SIZE;a++) {
for(int b=0;b<T_SIZE;b++) {
//Store the current character in the next spot in the array
t_type[a][b] = (char)fgetc(fp);
}
}
//Close the file!
fclose(fp);
return TRUE;
}
Now, everything works how it should when my text file reads:
Code:
0000000000001000000000000
Which places grass with one square of dirt in the center.
But when I try:
Code:
00000
00000
00100
00000
00000
The dirt is no longer in the middle because the invisible \n character at the end of every line is treated as part of the map.
My question is, how can I make the function ignore the \n character?