C++ SDL fscanf功能问题
我创建了一个函数,用于加载表单的一个非常基本的映射文件:C++ SDL fscanf功能问题,c++,sdl,scanf,C++,Sdl,Scanf,我创建了一个函数,用于加载表单的一个非常基本的映射文件: 1:1 2:1 1:1 2:2 2:2 2:2 ... ................... 2:1 1:1 然而,当使用fscanf读取文件时,我得到了一些非常奇怪的行为 查看我为读取地图而设置的文件变量,文件的“流”的基元素似乎已经完美地读取了文件。但是,文件的“流”的\u ptr缺少第一个数字和最后一个数字。因此,它被解读为: :1 2:1 1:1 2:2 2:2 2:2 ... ................... 2:1
1:1 2:1 1:1 2:2 2:2 2:2 ...
................... 2:1 1:1
然而,当使用fscanf
读取文件时,我得到了一些非常奇怪的行为
查看我为读取地图而设置的文件
变量,文件
的“流”的基
元素似乎已经完美地读取了文件。但是,文件的“流”的\u ptr
缺少第一个数字和最后一个数字。因此,它被解读为:
:1 2:1 1:1 2:2 2:2 2:2 ...
................... 2:1 1:
并且正在生成一个错误
以下是我的功能:
/**
* loads a map
*/
bool Map::LoadMap(char* tFile)
{
FILE* FileHandle = fopen(tFile, "r"); // opens map file for reading
if (FileHandle == NULL) // returns if the map file does not exist
return false;
for(int Y = 0; Y < MAP_HEIGHT; Y++) // iterates through each row
{
for(int X = 0; X < MAP_WIDTH; X++) // iterates through each column
{
Node tNode; // temp node to put in the map matrix
int tTypeID = 0;
int tNodeCost = 0;
fscanf(FileHandle, "%d:%d", tTypeID, tNodeCost);
tNode.SetPosition(X, Y);
tNode.SetType(tTypeID);
tNode.SetNodeCost(tNodeCost);
mMap[X][Y] = tNode; // inserts temp node into list
}
fscanf(FileHandle, "\n");
}
fclose(FileHandle);
return true;
}
/**
*加载地图
*/
boolmap::LoadMap(char*tFile)
{
FILE*FileHandle=fopen(tFile,“r”);//打开地图文件进行读取
if(FileHandle==NULL)//如果映射文件不存在,则返回
返回false;
for(int Y=0;Y
为什么会发生这种情况?您需要将变量的地址传递给fscanf()
:
建议检查fscanf()
的返回值以确保成功:
// fscanf() returns the number of assignments made or EOF.
if (2 == fscanf(FileHandle, "%d:%d", &tTypeID, &tNodeCost))
{
}
您需要将变量的地址传递给fscanf()
:
建议检查fscanf()
的返回值以确保成功:
// fscanf() returns the number of assignments made or EOF.
if (2 == fscanf(FileHandle, "%d:%d", &tTypeID, &tNodeCost))
{
}