فشل ifstream C ++، لماذا هذا الخط لن حيث أنه من المفترض أن؟
سؤال
وأريد أن جعل خط ملحوظ مع // هذا خط ينبغي أن تكون PRINTING بذل كل شيء، والذي هو طباعة القيم كثافة العمليات بين "مرادفات" و "المتضادات".
وهذا هو ملف نصي:
و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;
في خط "مرادفات" استخراج myId
فشل ويحدد failbit
على تيار، والذي يسبب عمليات الاستخراج التالية إلى فشل أيضا. لديك لإعادة سيطرة الدولة الخطأ قبل استخراج عناصر أخرى (مثل كلمة "مرادفات") من تيار:
in.clear();
نصائح أخرى
أولا، تشغيل تحذيرات مترجم. وقد يكون من المفيد أن تجد بعض الأشياء التي تظن أنها موافق ولكن الذي حقا ليست كذلك. على سبيل المثال، يجب الوظائف مع أنواع الإرجاع غير void
-دوما بإرجاع شيء. إذا لم يفعلوا ذلك، ثم هو سلوك برنامجك غير معروف، ويتضمن سلوك غير معرف "العمل تماما كما تريد، باستثناء بعض فرق دقيق في وقت لاحق في هذا البرنامج." إذا كنت تستخدم ز ++، خيار التحذيرات هو -Wall
.
والثانية، لاحظ أنه ليس مجرد الخط المميز ليست قيد التشغيل. و<م> وظيفة pushSynonyms
كامل م> لم يحصل يسمى. وصفك غطت كيفية استخدام المصحح حتى الآن؟ إذا كان الأمر كذلك، ثم النظر في استخدامه. إن لم يكن، ثم محاولة وضع بعض التصريحات "cout
" في البرنامج حتى تستطيع أن ترى بالضبط إلى أي مدى يحصل البرنامج قبل أن يذهب على نحو خاطئ.
وثالثا، لاحظ أنه عندما قرأ تيار الفشل يحدث، يتم تعيين تيار في فشل بعض الشيء. حتى مسحها (<لأ href = "https://stackoverflow.com/questions/456357/c-ifstream-failure-why-is-this-line-not-going-where-its-supposed-to#456390" > كما هو موضح من قبل لك شيء في الإجابة )، يمكن أن يحدث أي استخراج المزيد من أن تيار، لذلك كل استخدامات أخرى لل>>
وgetline
ستفشل.
وفعلتم أي الطباعة التشخيص؟ على سبيل المثال، ما هو synsAux.size()
؟ هل راجعت ما هو في synline
قبل البدء في معالجة ذلك؟ هل راجعت الأرقام التي يتم جمعها من تيار الإدخال؟