-1

I am creating an object called SpellChecker that corrects the spelling of words in a string.

To check if the words are spelled correctly and if not to correct them, I have a text file of correct words (one per line). I also have a text file of words that are misspelled and their corrections separated by a tab.

My issue is reading in my text file. I have created an if statement to see if my file opens successfully. However, I believe my file should be readable and it is not. I am trying to find out why this is happening.

Here is my SpellChecker constructor:

SpellChecker::SpellChecker(string tempLanguage, string correctWordsFile,string wordCorectionsFile){

language=tempLanguage;

ifstream istream;

istream.open(correctWordsFile);

if(!istream.is_open()){
  cout << "Error opening " << correctWordsFile << endl;
}

int count=0;
string temp;

while(!istream.eof()){
  getline(istream,temp);
  correctWords[count] = temp;
  count++;

}

numCorrectWords = count;

istream.close();

istream.open(wordCorectionsFile);

if(!istream.is_open()){
  cout << "Error opening " << wordCorectionsFile << endl;
}

int j=0;
int i=0;
char temp2;

while(!istream.eof()){

  istream.get(temp2);

  if(temp2 == '\t'){
    j++;
  }
  else if(temp2 == '\n'){
    i++;
    j = 0;
  }
  else
    wordCorections[i][j] += temp2;
}

numwordCorrections = i;

istream.close();
}

Here is my main:

int main(){
  SpellChecker spellCheck("English","CorectWords.txt","WordCorections.txt");
  spellCheck.viewCorrectWords();
  spellCheck.viewCorrectedWords();
  spellCheck.setEnd('~');
  spellCheck.setStart('~');
  cout << spellCheck.repair("I like to eat candy. It is greatt.");
}

The terminal returns:

"Error opening CorectWords.txt"

How can I solve this problem?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
lls12
  • 1
  • 1
  • Don't call your variable "istream". There is a `std::istream`, and your naming may conflict with this. – PaulMcKenzie Nov 02 '17 at 04:00
  • Try using an absolute pathname for the filenames. – Barmar Nov 02 '17 at 04:04
  • You will want to review [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – David C. Rankin Jun 20 '18 at 00:20

1 Answers1

0

The call to library function is_open() is returning false, which could be due to one of many reasons.
Ensure that :
1. You have used correct name of the data file.
2. The data file is in the same folder as the executable of your program.
3. It has been closed by any previous program that read it.

Gaurav Singh
  • 456
  • 6
  • 17