过去 4 个小时左右我一直在研究这个问题,但不知道该怎么做。我正在将我的 Game of Life 移植到 C,但无法让 FileIO 正常工作。输入文件的格式为:
Game 1: Pattern Name
10 20
// Pattern here
Game 2: Pattern Name
15 25
// Pattern here
依此类推,直到文件结束。10
我想要做的是打印游戏,并为第一个游戏创建一个大小的多维数组20
,然后将模式存储在这个数组中。这是我到目前为止所拥有的:
void fileIO() {
FILE *file;
char buffer[BUFFER_SIZE];
int rows = 0, cols = 0;
file = fopen("input.txt", "r");
if(file == NULL) {
printf("Error opening file.");
} else {
while(fgets(buffer, BUFFER_SIZE, file) != NULL) {
if(strstr(buffer, "Game") != NULL) {
printf("%s", buffer);
} else {
sscanf(buffer, "%d%d", &rows, &cols);
}
}
fclose(file);
}
}
这就是我碰壁的地方,遇到了一系列问题,
Creating a dynamic global multi-dimensional array
Preventing the buffer from reading into the next game
我认为最好的方法是为每个游戏创建一个结构数组,例如,
struct game {
char board[][];
};
struct game games[];
但是,我不知道如何动态设置每个行和列的数量的参数。