Advertisement

File loading problems.

Started by February 24, 2002 05:23 PM
3 comments, last by xria 22 years, 9 months ago
I am having troubles with a function I am writing. The purpose of the function is to read a txt file that contains a list of bmp filenames into an array, and then pass these names into a LoadGlTexture function so the files can be loaded. LoadGlTexture takes a char * filename as a parameter. My txt file that I am reading from looks like this: 3 "texture1.bmp" "texture2.bmp" "texture3.bmp" I get assertion errors and sometimes my visual c++ compiler crashes when I run this. I can simply type a filename into the first parameter of LoadGlTextures and it will run fine, but when I try to pass in my Texturefiles[] as a parameter I get those errors. I could use some assistance on getting this working, and maybe some help on just making it cleaner. bool map::loadmap(string mapfile,char *texturefile) { //mapfile is loaded up here------------ //not important since I got this part of the function working // already----------------------------- //LOAD TEXTURE FILES INTO AN ARRAY------------------------ //this part of the function is the trouble area. char *Texturefiles[30]; //used to store filenames int loop=1; //used in loop ifstream tin; tin.open(texturefile); //open the txt file with the //names of the texture //bmps if(tin.fail()) { return false; } int texturecount; //stores first line of file //which tells how many lines //need to be read tin>>texturecount; //read in first line while(loop<=texturecount){ //load textures into slots //1-3 in Texturefiles tin >> Texturefiles[loop]; LoadGLTexture(Texturefiles[loop],loop); //problems happen loop++; } tin.clear(); tin.seekg(NULL, ios::beg); tin.close(); }//end map function
Your problem is that you never initialize the pointers in your char* array. Try adding the following lines after the declaration of TextureFiles:
  for (int i = 0; i < 30; i++)    TextureFiles[i] = new char[128];  


Not the most elegant solution, but it should work.


-Neophyte


- Death awaits you all with nasty, big, pointy teeth. -
Advertisement
hi, is that really necessary? why? oh and why char[128]?

Edited by - mickey on February 25, 2002 7:40:28 AM
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Appreciate the info neo, it works fine now.
Mickey:
After the declaration
char* TextureFiles[30];
TextureFiles is now an array of 30 char-pointers. However the pointers doesn''t actually point to anything, because they haven''t been initialized yet.
Initialization is done with the
TextureFiles = new char[128];

After the loop every pointer in the TextureFiles array now points to 128 chars of available memory.
I used 128 just to be relatively certain that no filenames where longer than the memory pointed to. If you can be certain that no filename is longer than 31 characters it could be replaced with new char[32] instead.

-Neophyte


- Death awaits you all with nasty, big, pointy teeth. -

This topic is closed to new replies.

Advertisement