• Advertisement

Archived

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

File loading problems.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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. -

Share this post


Link to post
Share on other sites
hi, is that really necessary? why? oh and why char[128]?

Edited by - mickey on February 25, 2002 7:40:28 AM

Share this post


Link to post
Share on other sites
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. -

Share this post


Link to post
Share on other sites

  • Advertisement