【问题标题】:C++ ifstream failure, why is this line not going where it's supposed to?C++ ifstream 失败,为什么这条线不去它应该去的地方?
【发布时间】:2009-01-19 02:14:32
【问题描述】:

我想让标有 // THIS LINE SHOULD BE PRINTING 的行做它的事情,即打印“同义词”和“反义词”之间的 int 值。

这是文本文件:

字典.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;
      }

【问题讨论】:

  • 这是家庭作业,不是吗?你有没有向你的导师寻求帮助?毕竟,这就是他或她的报酬。
  • 我在委内瑞拉加拉加斯写这篇文章。我的导师没有为此获得报酬,事实上,我的导师不知道如何使用 ifstream .. 这家伙提倡 scanf。

标签: c++ ifstream


【解决方案1】:

问题似乎出在这里:

in>>myId>>word;

在“同义词”行上,myId 的提取失败并在流上设置 failbit,这会导致以下提取也失败。在从流中提取更多元素(如单词“同义词”)之前,您必须重置错误控制状态:

in.clear();

【讨论】:

  • 谢谢,我在 while (inStream >>aword && (!(aword=="synonyms"))){wordVector.push_back(aword);} 之后放置了明确的声明,它允许阅读,它到达了 pushSynonyms 函数,尽管现在打印越来越垃圾了。
【解决方案2】:

首先,打开编译器警告。它可能会帮助您找到一些您认为可以但实际上不是的事情。例如,具有非void 返回类型的函数应该总是返回一些东西。如果他们不这样做,那么你的程序的行为是未定义的,未定义的行为包括“完全按照你的意愿工作,除了程序后面的一些细微差别”。如果您使用的是 g++,则警告选项为 -Wall

其次,请注意,不只是突出显示的行没有运行。 整个pushSynonyms 函数永远不会被调用。您的课程是否涵盖了如何使用调试器?如果是这样,那么考虑使用它。如果没有,请尝试在您的程序中添加一些“cout”语句,这样您就可以准确地看到您的程序在出错之前能走多远。

第三,注意当流读取失败时,流的失败位被设置。在您清除它 (as shown by sth's answer) 之前,无法从该流中进行进一步提取,因此所有进一步使用 &gt;&gt;getline 都将失败。

【讨论】:

    【解决方案3】:

    您是否进行过任何诊断打印?例如,synsAux.size() 是什么?在开始处理之前,您是否检查过 synline 中的内容?您是否检查过从输入流中收集了哪些数字?

    【讨论】:

      猜你喜欢
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      相关资源
      最近更新 更多