我正在尝试执行这段代码,该代码应该读取带有 char 的文件,以便将它们存储在 char 变量的双表中。终端输出以 319 结束。
问题似乎发生在循环的末尾。读取文件似乎没有任何问题,但退出循环似乎没有任何问题。根据错误信息,似乎是库的问题。这就是为什么,我卸载了所有 C 库并重新安装它们,但它没有解决问题。我使用 printf() 尝试调试,发现退出循环时出现问题。最后的输出是 i=319。由于我不理解错误消息,我没有潜在的解决方案。
这是代码:
#define LOADMAP_H
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
const int mX=460;
const int mY=320;
float square_size_width=3;
float square_size_height=3;
char **map;
void loadmap()
//fonction qui ouvre le fichier txt et charge la carte dans le tableau
{
map = malloc(sizeof(char *) * mY);
FILE *f = NULL;
f = fopen("map1.txt", "r");
if(f == NULL)
{
printf("file empty");
}
char c;
int i=0;
int j = 0;
for(i = 0; i < mY; i++)
{
*(map + i) = malloc(sizeof(char *) * mX);
for(j = 0; j < mX; j++)
{
c = fgetc(f);
map[i][j] = c;
printf("%c", map[i][j]);
}
printf("%i\n",i);
}
fclose(f);
}
这是 makeFile 编译文件的代码 你好,我使用 gcc 和 make 文件进行编译,这是编译代码
CFLAGS = -Wall -std=c99 -g
GL = -lglut -lGL -lGLU
C = gcc
SRC = $(wildcard *.c)
OBJ = $(SRC:.c=.o)
DEPS = $(SRC:.c=.h)
#Variables spéciales
#$@ fait référece àla cible
#$< nom première dépendance
#$^ liste deépendance
#$? liste dépendance plus récents que la cible
#$* nom fichier sans son extension
all: exec
%.o: %.c $(DEPS)
$(C) -c $< -o $@ $(CFLAGS) $(GL)
#on crée tous les fichiers .o en partant de tous les fichiers.c
exec: $(OBJ)
$(C) -o $@ $^ $(CFLAGS) $(GL)
#ici, $@=exec
writeMap: writeMap.o
$(C) -o $@ $^ $(CFLAGS) $(GL)
clean:
rm -f *.o
cleanall: clean
rm -f exec
rm -f *~
我使用 gcc 作为编译器。