Valhalla Legends Forums Archive | C/C++ Programming | Another! Segmentation Fault

AuthorMessageTime
Brandon
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:

[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();
}
[/code]

getWord() function:

[code]

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;
}[/code]
October 31, 2006, 8:42 PM
warz
It would be best to initialize your char pointers to something prior to moving data there.
October 31, 2006, 9:12 PM
Brandon
Oh, mmk.

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

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

  in.open(file.c_str());
[/code]

To this:

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

  in.open(file);
[/code]
October 31, 2006, 9:44 PM

Search