I'm having a slight problem. This particular function is crashing caused by the Loadfile ifstream being opened.

void LoadChar(Entity players[], char saveNum)
{
     string filename, saves;
     saves = saveNum;
     filename = "save/Save" + saves + ".sav";
     char *file;
     file = const_cast<char*>(filename.c_str());
    
     ifstream Loadfile(file, ios::in);  //this little diddy right here

     if (!Loadfile)
     {
       cout<<"Error saving player data!"<< endl;
       return;
     }
     if ( Loadfile.is_open() )
     {
              try
              {
                  Loadfile >> players[0].name;
                  Loadfile >> players[0].health;
                  Loadfile >> players[0].strength;
                  Loadfile >> players[0].inRoom ;
                  Loadfile.getline(players[0].descrip, 56);
                  Loadfile >> players[0].isNPC;
                  Loadfile >> players[0].hasScript;
              }
              catch (exception&)
              {
                  cerr<<"Something went horribly wrong!"<< endl;
              }
     }
     Loadfile.close();

}

I surrounded the problem line with a try/catch block and it catches as a st9exception which is about as useful as telling me something went wrong.

Anyone have any ideas on why this is happening, I had the same problem with the SaveChar function and I completely forgot how I fixed it.

Is this the first function called from main()?
Or is there a lot of other stuff which could have screwed up, and here is where you get to notice?

Try the code in isolation (a simple main, this function, and not much else). Does it work? If so, then the problem is the overall environment in which the function runs, not the function itself.

> file = const_cast<char*>(filename.c_str());
Why are you casting away the const-ness here?
Doesn't ifstream Loadfile(filename.c_str(), ios::in); work?

EDIT:
I found the source of the problem. the getline call is causing the crash. I ran through and tested all the input and that is the only one causing a crash. So now that I've pinpointed the problem I just have to solve it. So what could be causing it to crash for Loadfile.getline(player.descrip, 56)?

> file = const_cast<char*>(filename.c_str());
Why are you casting away the const-ness here?
Doesn't ifstream Loadfile(filename.c_str(), ios::in); work?

nope, it's an invalid cast from const char* to char* .

I have the identical function for SaveChar though it's an output stream and it works fine.

void SaveChar(Entity &player, char saveNum)
{
     string filename;
     string saves;
     saves = saveNum;
     filename = "save/Save" + saves + ".sav";
     char *file;
     file = const_cast<char*>(filename.c_str());
     ofstream SaveFile;
     SaveFile.open(file, ios::out);     
     
     if (!SaveFile)
     {
       cout<<"Whoops, something went wrong!"<< endl;
       return;
     }
         if ( SaveFile.is_open() )
         {
              try
              {
                  SaveFile << player.name << endl;
                  SaveFile << player.health << endl;
                  SaveFile << player.strength << endl;
                  SaveFile << player.inRoom << endl;
                  SaveFile << player.descrip << endl;
                  SaveFile << player.isNPC << endl;
                  SaveFile << player.hasScript << endl;
              }
              catch (exception&)
              {
                  cerr<<"Something went horribly wrong!"<< endl;
              }
         }
     SaveFile.close();
}

This works completely fine.

As per the function being called, I've edited it on a few occasions where the outside variables are ignored and I use static arguments to the constructor. IE. ifstream Loadfile("save/Save1.sav", ios::in)

> nope, it's an invalid cast from const char* to char*.
How can this be when you can also do ifstream Loadfile("save/Save1.sav", ios::in) This works fine for me

#include <fstream>
#include <string>
#include <iostream>
using namespace std;
 
int main ( ) {
    string file = "hello";
    ifstream inFile( file.c_str() );
    return 0;
}

$ g++ -W -Wall -ansi -pedantic foo.cpp

Casually removing the const-ness will hurt you at some point. If you end up accidentally modifying something which originally came from c_str(), you're screwed.


> So what could be causing it to crash for Loadfile.getline(player.descrip, 56)?
In the absence of your struct, I'm going to say that it's a bare "char *" variable that has not been allocated at least 56 bytes of space.
Or it's an array which isn't at least 56 bytes.

This article has been dead for over six months. Start a new discussion instead.