C ++ ifstreamの失敗、なぜこの行は想定された場所に行かないのですか?
質問
//でマークされた行に、この行を印刷する必要があります。<!> quot; synonyms <!> quot;および<!> quot; antonyms <!> quot;。
これはテキストファイルです:
dictionary.txt
1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class WordInfo{
public:
WordInfo(){}
~WordInfo() {
}
int id() const {return myId;}
void readWords(istream &in)
{
in>>myId>>word;
}
void pushSynonyms (string synline, vector <WordInfo> wordInfoVector)
{
stringstream synstream(synline);
vector<int> synsAux;
int num;
while (synstream >> num) synsAux.push_back(num);
for (int i=0; i<synsAux.size(); i++){
cout<<synsAux[i]<<endl; //THIS LINE SHOULD BE PRINTING
}
}
void pushAntonyms (string antline, vector <WordInfo> wordInfoVector)
{
}
//--dictionary output function
void printWords (ostream &out)
{
out<<myId<< " "<<word;
}
//--equals operator for String
bool operator == (const string &aString)const
{
return word ==aString;
}
//--less than operator
bool operator <(const WordInfo &otherWordInfo) const
{ return word<otherWordInfo.word;}
//--more than operator
bool operator > (const WordInfo &otherWordInfo)const
{return word>otherWordInfo.word;}
private:
vector <int> mySynonyms;
vector <int> myAntonyms;
string word;
int myId;
};
//--Definition of input operator for WordInfo
istream & operator >>(istream &in, WordInfo &word)
{
word.readWords(in);
}
//--Definition of output operator
ostream & operator <<(ostream &out, WordInfo &word)
{
word.printWords(out);
}
int main() {
string wordFile;
cout<<"enter name of dictionary file: ";
getline (cin,wordFile);
ifstream inStream (wordFile.data());
if(!inStream.is_open())
{
cerr<<"cannot open "<<wordFile<<endl;
exit(1);
}
vector <WordInfo> wordVector;
WordInfo aword;
while (inStream >>aword && (!(aword=="synonyms")))
{
wordVector.push_back(aword);
}
int i=0;
while (i<wordVector.size()){
cout<<wordVector[i]<<endl;
i++;
}
vector <int> intVector;
string aLine; //suspect
// bad statement?
while (getline(inStream, aLine)&&(aLine!=("antonyms"))){
aword.pushSynonyms(aLine, wordVector);
}
system("PAUSE");
return 0;
}
解決
問題はここにあるようです:
in>>myId>>word;
<!> quot;同義語<!> quot; myId
の抽出が失敗し、ストリームにfailbit
を設定します。これにより、次の抽出も失敗します。ストリームからさらに要素(単語<!> quot; synonyms <!> quot;など)を抽出する前に、エラー制御状態をリセットする必要があります:
in.clear();
他のヒント
最初に、コンパイラの警告をオンにします。それは、あなたが大丈夫だと思うが実際にはそうではないものを見つけるのに役立つかもしれません。たとえば、void
以外の戻り型を持つ関数は常に何かを返す必要があります。そうでない場合、プログラムの動作は未定義であり、未定義の動作には<!> quot;プログラム内の微妙な違いを除き、希望どおりに動作することが含まれます。<!> quot; g ++を使用している場合、警告のオプションは-Wall
です。
第二に、実行されていないのは強調表示された行だけではないことに注意してください。 全体pushSynonyms
関数が呼び出されることはありません。あなたのクラスは、デバッガの使用方法をまだカバーしましたか?もしそうなら、それを使用することを検討してください。そうでない場合は、いくつかの<!> quot; cout
<!> quot;プログラム内のステートメントを使用して、プログラムがエラーになるまでの正確な距離を確認できます。
第三に、ストリームの読み取りエラーが発生すると、ストリームの失敗ビットが設定されることに注意してください。クリアするまで( sthの回答で示されているように)、そのストリームからそれ以上抽出することはできません。したがって、>>
およびgetline
の使用はすべて失敗します。
診断印刷をしましたか?たとえば、synsAux.size()
とは何ですか?処理を開始する前にsynline
の内容を確認しましたか?入力ストリームから収集される数値を確認しましたか?