我试图用strtok和strcmp解析文件。我是Eclipse的新手,在Linux上开发并且遇到了让一切工作正常的问题。我唯一能联系的是-lm,所以我不确定这是某种类型的链接问题还是什么。
char fileBuffer[2048];
for(i=0;i < 7;i++)
{
sprintf(filepath,"%s/Room%d.txt",directory,i);
FILE* file = fopen(filepath,"r");
if(file == NULL)
{
printf("Expected file @ %s/Room%d.txt does not exist. Cannot complete loading game.",directory,i);
exit(1);
}
fseek(file,0,SEEK_END);
int size = ftell(file);
assert((size + 1) < sizeof(fileBuffer));
fseek(file,0,SEEK_SET);
fread(fileBuffer,sizeof(char),size,file);
fclose(file);
fileBuffer[size]='\0';
const char* delimiters = ":\n";
//!!!! This returns garbage that crashes the program when
//submitted to strcmp for comparisons.
const char* t = strtok(fileBuffer,delimiters);
while(t != NULL)
{
if(strcmp(t,"ROOM NAME") == 0)
{
t = strtok(NULL,delimiters);
RoomIndex[i] = NameToIndex(t + 1);
}
else if(strcmp(t,"ROOM TYPE") == 0)
{
t = strtok(NULL,delimiters);
for(j = 0; j < 3;j++)
{
if(strcmp(t + 1,RoomType[j]) == 0)
{
rooms[i].RoomType = j;
break;
}
}
}
t = strtok(NULL, delimiters);
}
}