所以,我无法读取我的文件。
我想读这个文件;
1
0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0
0 0 0 2 0 0 0 0
0 0 0 2 1 0 0 0
0 0 1 2 2 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
第一个 1 是我的播放器,而数组是我的游戏板。
所以我正在尝试使用这段代码阅读所有这些内容:
void initialize_file(Game *game)
{
int i,j;
FILE *file = fopen("save.sav", "r");
if(file==NULL)
{
printf("Can't read file.... \n");
}
else
{
game->player=(fgetc(file)-'0');
printf("Player %d loaded\n",jeu->player);
fgetc(file); //jump the two lines after player
fgetc(file);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
Game->game[i][j] = (fgetc(file)-'0');
printf("value : %d \n",Game->game[i][j]);
fgetc(file);
}
fgetc(file);
}
}
}
我的问题是有时我会得到数组之外的值,例如 -16 和 -38,它们分别指向 Space 和 LF。 所以我的问题是:我怎样才能避免遇到这些 -16 和 -38?
非常感谢,对于任何英文错误,我提前表示歉意
最佳答案
每次执行 c = fgetc(..)
时,请在使用前检查 c != EOF
和 isdigit(c)
。
明智的话,c
应该是一个int
。但当然每个人都知道这一点。
关于c - 在 C 中的行尾后读取问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21501907/