• Welcome to Valhalla Legends Archive.
 

Another! Segmentation Fault

Started by AntiVirus, October 31, 2006, 02:42 PM

Previous topic - Next topic

AntiVirus

God!  I get these damn things a lot!

Anyway, I can usually figure them out, but this one I have no idea. 

Here is my code:


void dsply_chg()
{
  ifstream in;
  char story[150];
  char* target;
  char* word;

  in.open("index.dat");

  cout<<endl;
  cout<<"Changes have been made!";
  cout<<endl;
  cout<<endl;
  while(in >> story)
  {
    if(target = strstr(story, "*nouns*"));
    if(target != NULL)
    {
      word = getWord("nouns.dat");
      strncpy(target, word, 15);
    }
    if(target = strstr(story, "*adj*"));
    if(target != NULL)
    {
      word = getWord("adj.dat");
      strncpy(target, word, 15);
    }
    cout<<story<<" ";
  }
  in.close();
}


getWord() function:



char* getWord(const string file)
{
  ifstream in;
  int data_value = 0;
  char list[250];
  char* word;

  in.open(file.c_str());
  if(!in)
  {
    cout<<"Can't connect to "<<file<<".";
    exit(1);
  }
  else
  {
    in >> list;
    data_value = (rand() % 9) +1;
    for(int i = 0; i < data_value; i++)
    {
      in >> word;
    }
  }
  in.close();
return word;
}
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

warz

It would be best to initialize your char pointers to something prior to moving data there.

AntiVirus

Oh, mmk.

Well, I fixed it.  And I believe the only thing I did was change this:


char* getWord(const string file)
{
  ifstream in;
  int data_value = 0;
  char list[250];
  char* word;

  in.open(file.c_str());


To this:


char* getWord(const char* file)
{
  ifstream in;
  int data_value = 0;
  char list[250];
  char* word;

  in.open(file);
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus