2017-11-02 3 views
-1

文字列内の単語のスペルを修正するSpellCheckerというオブジェクトを作成しています。単語の綴りが正しいかどうかをチェックし、訂正しない場合は、行ごとに正しい単語のテキストファイルがあります。また、単語のテキストファイルのスペルが間違っていて、その訂正がタブで区切られています。私の問題はテキストファイルを読むことです。私はファイルが正常に開くかどうかを調べるためにif文を作成しましたが、私のファイルは読み込み可能でなければならないと信じています。私はなぜこれが起きているのかを探そうとしています。ここifstream C++を使用してテキストファイルを開くときにエラーが発生する11

は私のスペルチェッカーのコンストラクタはここ

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(); 

}

である私のメインです:事前に

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."); 
} 

ターミナルに戻り、 "CorectWords.txtを開くエラー"

感謝。

+0

変数 "istream"を呼び出さないでください。 'std :: istream'があり、あなたの命名はこれと矛盾するかもしれません。 – PaulMcKenzie

+0

ファイル名に絶対パス名を使用してみてください。 – Barmar

答えて

0

ライブラリ関数is_open()の呼び出しがfalseを返すことは、多くの理由の1つに起因する可能性があります。
以下を確認してください。
1.データファイルの正しい名前を使用しています。
2.データファイルは、プログラムの実行可能ファイルと同じフォルダにあります。
3.それを読んだ以前のプログラムによって閉じられました。

関連する問題