开发者

Reading strings from file into array

开发者 https://www.devze.com 2023-03-01 00:44 出处:网络
Hey. I\'m trying to read strings into an array from a file that contains a list of words. This is so that I can check to see if strings are a real word by seing is they exist inside my array. I have e

Hey. I'm trying to read strings into an array from a file that contains a list of words. This is so that I can check to see if strings are a real word by seing is they exist inside my array. I have everything working except the compare. My binary search even passes by the word in question. When it compares the two words which are exactly the same, it still returns false. I think the problem is probably in the way I am pulling the words in because the string.compare() function works fine normally. Here is that code. I would love some help. Thanks.

  ifstream dictFile;
  dictFile.open("dictionary.txt");
  if (!dictFile) // testing if file open
    {
      cout << "Error opening dictionary file" << endl;
    }
  int index = 0; // dictionary must progress start at line 1
  while(!dictFile.eof())
    {
      getline(dictFile,dictionary[index]);
      index++;
    }
  dictFile.close();

Is there anything just plain wrong about how I am doing this?

EDIT Here is the comparison code as well

bool database::is_word(string word)
{
  int ii;
  int comp;
  int min = 0;
  int max = dictSize;
  // this will go into the dictionary and look for the word
  // it uses a binary search pattern
while (min<=max)
    {
      ii = (min+max)/2;
      comp = word.compare(dictionary[ii]);
      cout <<dictionary[ii];
      if (comp==0)
    {
      cout << word<< " is a word!" << endl;
      return 1;
    }
      else if (comp < 0)
    {
      max = ii-1;
    }
      else
    {
      min = ii+1;
      }
      }
 开发者_如何转开发cout << word << " is NOT a word!" << endl;
  return 0;
}


Not the eof() function again! You want:

while( getline(dictFile,dictionary[index]) ) {
  index++;
}

(assuming dictionary is something sensible, which it might not be) because eof() does not predict if the next read will work.

And where oh where are people picking up this use of eof() from? It's like a disease!


This is how I'd do the whole program, if my goal were terseness and not performance.

// read the dictionary 

vector<string> dictionary;
{
  ifstream dictionary_file("dictionary.txt");
  istream_iterator<string> begin(dictionary_file);
  istream_iterator<string> end;
  while( begin != end )
    dictionary.push_back( *begin++ );
  sort( dictionary.begin(), dictionary.end() );
}

// read the input file and test against the dictionary

{
  ifstream input_file("input.txt");
  istream_iterator<string> begin(input_file);
  istream_iterator<string> end;
  while( begin != end )
  {
    string input = *begin++;
    vector<string>::iterator it = lower_bound( dictionary.begin(), dictionary.end(), input );
    if( it != dictionary.end() && *it == input )
      cout << input << " found!" << endl;
    else
      cout << input << " not found!" << endl;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消