我试图用文本文件中的字符填充二维数组(mapLayout).
当我使用printf输出字符时,一切看起来都很好,但是将数字添加到数组的实际行似乎导致了崩溃.
#include <stdio.h>
#include <stdlib.h>
void createMap();
//height of file being read
int mapHeight, mapWidth = 20;
char mapLayout[20][20];
int main()
{
createMap();
return 0;
}
//read in string from file and populate mapLayout with chars
void createMap(){
FILE *file = fopen("map.txt", "r");
int col, row = 0;
int c;
if (file == NULL)
return NULL; //could not open file
while ((c = fgetc(file)) != EOF)
{
printf("%c", c);
printf("\nx:%d, y:%d\n", col, row);
if(c == '\n'){
row++;
col = …Run Code Online (Sandbox Code Playgroud)