#### Archived

This topic is now archived and is closed to further replies.

This topic is 5448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just recently wrote this function to load in files containing level information for a 2-D side scroller game I'm working on, where anything with an m_ in front of it is a member variable of the mm9Core class, and the first character following the underscore is the variable type:
MM9ErrorType mm9Core::LoadLevelInfo()
{
char*		szLevelFileName = NULL;
char*		szBossFileName = NULL;
char*		szBossNameFileName = NULL;
int		iNameWidth, iNameHeight;
fstream		kLevelFile;

//Choose appropriate level file to load with respect to chosen boss.
switch(m_iSCursorBossPos)
{
case 0:
szLevelFileName = "LevelInfo/SampleLevel2.txt";
break;
default:
return LEVELFILENOTFOUND;
}

kLevelFile.open(szLevelFileName, ios::in);

//clear all of the arrays so that they can store new level data

if(m_szTileFiles != NULL)
delete [] m_szTileFiles;

if(m_iTileMap != NULL)
delete [] m_iTileMap;

if(m_szNoCollide != NULL)
delete [] m_szNoCollide;

if(m_szWaterZone != NULL)
delete [] m_szWaterZone;

if(m_szKillZone != NULL)
delete [] m_szKillZone;

if(kLevelFile.is_open())
{
//read level width and height in tiles
kLevelFile>>m_iLvlWidth>>m_iLvlHeight;

//read in the number of distinct tiles
kLevelFile>>m_iNumTiles;

m_szTileFiles = new char*[m_iNumTiles];

for(int i = 0; i < m_iNumTiles; i++)
{
m_szTileFiles[i]       = new char[MAXFILENAMELEN];
}

for(i = 0; i < m_iNumTiles; i++)
{
kLevelFile>>m_szTileFiles[i]    ;
}

kLevelFile>>m_iNumNoCollide;

if(m_iNumNoCollide != 0)
m_szNoCollide = new char[m_iNumNoCollide*2];

for(i = 0; i < m_iNumNoCollide; i+=2)
{
kLevelFile>>m_szNoCollide[i]     ;
m_szNoCollide[i+1] = ' ';
}

kLevelFile>>m_iNumWaterZone;

if(m_iNumWaterZone != 0)
m_szWaterZone = new char[m_iNumWaterZone*2];

for(i = 0; i < m_iNumWaterZone; i+=2)
{
kLevelFile>>m_szWaterZone[i]     ;
m_szWaterZone[i+1] = ' ';
}

kLevelFile>>m_iNumKillZone;

if(m_iNumKillZone != 0)
m_szKillZone = new char[m_iNumKillZone*2];

for(i = 0; i < m_iNumKillZone; i++)
{
kLevelFile>>m_szKillZone[i]     ;
m_szKillZone[i+1] = ' ';
}

m_iTileMap = new int* [m_iLvlHeight];

for (i = 0; i < m_iLvlHeight; i++)
{
m_iTileMap[i]      = new int [m_iLvlWidth];
}

for(i = 0; i < m_iLvlHeight; i++)
{
for(int j = 0; j < m_iLvlWidth; j++)
{
kLevelFile>>m_iTileMap[i][j]    ;
}
}

kLevelFile>>m_iStartX>>m_iStartY;

//!!!//
//INFORMATION FROM THE FILE NEVER GETS READ INTO THESE VARIABLES!!//
//IT IS AS IF THE FILE ENDED HERE//
kLevelFile>>szBossFileName;
kLevelFile>>szBossNameFileName;
kLevelFile>>iNameWidth>>iNameHeight;

//end of file-reading portion part of function
}

The level file I'm attempting to read in is this: -------------------------------------------------
10 8
5
Graphics/SquareMan/tile0.bmp
Graphics/SquareMan/tile1.bmp
Graphics/SquareMan/tile2.bmp
Graphics/SquareMan/tile3.bmp
Graphics/SquareMan/tile4.bmp
1
0
0
0
0 1 1 1 1 1 1 1 1 0
4 0 0 0 0 0 0 0 0 2
4 0 0 0 0 0 0 0 0 2
4 0 0 0 0 0 0 0 0 2
4 0 0 0 0 0 0 0 0 2
4 0 0 0 0 0 0 0 0 2
4 0 0 0 0 0 0 0 0 2
0 3 3 3 3 3 3 3 3 0
0 0
ABOs/mm9SquareManABO.txt
ABOs/mm9NameSquareManABO.txt
181 16

------------------------------------------------- The last 4 variables (the section of the code I marked with //!!!//) never receive any values; as if there is no more data left in the file to read. However, I investigated the file reading routine using peek() and after reading in the pair of 0s after that big matrix of ints, peek() returns ASCII character code 013, which is a carriage return. Once kLevelFile>>szBossFileName; is executed, peek() returns ASCII character code -1, which is garbage I guess. Also I tested for EOF after the pair of 0s, and also tested is_open() again and both returned that the end of the file was not reached, and that it was still open. Why can't I read in those two file names and the pair of integers at the end of the file? Am I missing something? I appreciate all help. **PLEASE NOTE** the forum form formatting (that's a mouthful) isn't too conducive to code; so anything regarding loop control variables (i in particular since, [i] is the code for italics) that looks incorrect is most likely just a formatting error _________________ ~Firestorm Fenix~ [edited by - FirestormFenix on August 13, 2003 12:45:47 AM]

##### Share on other sites
not too sure what the problem is, but i noticed that you opened the file in binary mode, why is that? you should process the file in ascii mode, seeing as it is purely text that you are reading in.

##### Share on other sites
Oh man, forgot about that. I was working with bitmaps earlier and I just typed that in by accident. I did however remove ios::binary from it just now, and I'm still having the same problem......

I'm going to fix the first post so it doesn't show that anymore.

[edited by - FirestormFenix on August 13, 2003 12:45:10 AM]

##### Share on other sites
hint.. use $source$ $/source$ (edit - minus the \)tags to post large chunks of code. I am only familiar with using FILE and fopen for text files, but have you tried reading back the data as you gather it to make sure you are where you think you are in the file? I know it sucks but sometimes I miss things and it is the only way I will find them, especially if the file gets much bigger than what youve already got. I use a file class I wrote to handle reading everything and I know it helps that if I get something unexpected it gives me the value and line number it occured on. I use a similar map file, heres what one of mine looks like: 20x15x2
MAPFILE NewMap.map~ NewMap0~ 20 15 2TILESET_BMP tileset0.bmp~ 32 32MAP_DATA   -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1     4    -1    -1    -1     4    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1     4    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1    -1 END_OF_FILE

[edited by - evillive2 on August 14, 2003 1:51:34 AM]

##### Share on other sites
My code would look nothing like that for the same function=)
I use fopen/fclose/sprintf for most of these files reading projects.

##### Share on other sites
ok, i looked again at the code and saw this

char* szBossFileName = NULL;
char* szBossNameFileName = NULL;

you are creating char pointers, but not allocating memory for them. so you can either:

char szBossFileName[100];

or better yet, seeing as you are using c++ streams...

String szBossFileName;

##### Share on other sites
Okay.

Kuladus - I tried what you said, and that solved the problem. I was thinking about that solution before, but I dismissed the idea because I was able to do this:
char*		szLevelFileName = NULL;//...szLevelFileName = "LevelInfo/SampleLevel2.txt";

I'm supposing it makes a difference when the data is coming from a file?

evillive2 - What do you mean by reading back the data that I gather, do you mean doing something such as outputting it to a console to check? I've mainly just been walking through with the debugger in VC++ to see what's going on. Actually, is there a way (I'm sure there's got to be) to output to the console in a Win32 project? I tried using std::cout but that didn't work. I'm a newb when it comes to Win32 stuff, so pardon my ignorance.
Oh, on a side note, I used $code$$/code$ in the first post since I've seen it used in phpBB forums to do the same thing. Using the source tags here is way better though, thanks.

chbfiv - What don't you like about what I've done? I'm just curious...improving my code is also something I'm always interested in doing.

_________________
~Firestorm Fenix~

[edited by - FirestormFenix on August 17, 2003 8:51:04 PM]

##### Share on other sites
char*		szLevelFileName = NULL;szLevelFileName = "LevelInfo/SampleLevel2.txt";

When people learn C, they need to first read a book about C, then read a book about pointers. then another book on pointers, then another book on pointers

repeat ad infinitum until they figure out why that works, and why the reading from a file does not work.

also, you need a .ignore(80 /*reasonable number*/, ''\n''); when switching from cin>>int to cin>>string.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×