even though this code is from a game engine i am making, the problem itself is base C++ oriented so i though i would post it here.
Code:
void readstr(FILE *f,char *string)
{
do
{
fgets(string, 255, f);
} while ((string[0] == '/') || (string[0] == '\n'));
return;
}
tMap LoadMap(char *szFileName)
{
tMap Map;
FILE *file;
char oneline[255]; // one line of text
int nNumTiles;
float x,y,u,v;
int tID,objID, nSol,nRows, nColumns;
int xSize,ySize;
file = fopen(szFileName,"rt");
readstr(file,oneline);
sscanf(oneline, "NUMTILES %i %i %i %i %i\n", &nNumTiles, &nRows, &nColumns, xSize, ySize);
Map.nNumRows = nRows;
Map.nNumColumns = nColumns;
Map.txSize = xSize;
Map.tySize = ySize;
Map.tTiles = new tTile[nRows * nColumns];
for(int nTiles =0; nTiles < nNumTiles; nTiles++)
{
for(int nVertice = 0; nVertice < 6; nVertice++)
{
readstr(file,oneline);
sscanf(oneline,"%f %f %f %f\n", &x,&y,&u,&v );
Map.tTiles[nTiles].vVertex[nVertice].x = x;
Map.tTiles[nTiles].vVertex[nVertice].y = y;
Map.tTiles[nTiles].vVertex[nVertice].u = u;
Map.tTiles[nTiles].vVertex[nVertice].v = v;
}
readstr(file,oneline);
sscanf(oneline,"%i %i %i\n", &tID, &objID,&nSol);
Map.tTiles[nTiles].tID = tID;
Map.tTiles[nTiles].objID = objID;
if(nSol == 1){
Map.tTiles[nTiles].solid = true;
}
else if(nSol == 0)
{
Map.tTiles[nTiles].solid = false;
}
}
fclose(file);
return Map;
};
ok, so, I have isolated the problem to fgets itself, not the loop that it is in by just using fgets by itself in the function. Why would fgets crash the program, when the file loads, and the only thing i find is that just by running the function, it crashes. I know the fgets works because it does load up the number of tiles(i did have a few error checking functions in there), however, it is the only thing i think could be causing this problem, because i have limited the function to only opening the file and using fgets and it crashes.