C++ ifstream 失败,为什么这条线没有到达它应该去的地方?

发布于 2024-07-11 08:17:19 字数 3578 浏览 6 评论 0原文

我想让标有 // THIS LINE SHOULD BE PRINTING 的行执行其操作,即打印“同义词”和“反义词”之间的 int 值。

这是文本文件:

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;
      }

I want to make the line marked with // THIS LINE SHOULD BE PRINTING do its thing, which is print the int values between "synonyms" and "antonyms".

This is the text file:

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;
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-07-18 08:17:20

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

Have you done any diagnostic printing? For example, what is synsAux.size()? Have you checked what is in synline before you start processing it? Have you checked which numbers are collected from the input stream?

辞旧 2024-07-18 08:17:19

问题似乎出在这里:

in>>myId>>word;

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

in.clear();

The problem seems to be here:

in>>myId>>word;

On the "synonyms" line the extraction of myId fails and sets failbit on the stream, which causes the following extractions to also fail. You have to reset the error control state before extracting further elements (like the word "synonyms") from the stream:

in.clear();
仅冇旳回忆 2024-07-18 08:17:19

首先,打开编译器警告。 它可能会帮助您找到一些您认为没问题但实际上不合适的事情。 例如,具有非 void 返回类型的函数应该始终返回一些内容。 如果不这样做,那么你的程序的行为是未定义的,未定义的行为包括“完全按照你想要的方式工作,除了程序稍后的一些细微差别”。 如果您使用的是 g++,则警告选项为 -Wall

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

第三,请注意,当发生流读取失败时,会设置流的失败位。 直到您清除它(如 sth 的答案所示),无法从该流中进行进一步的提取,因此 >>getline 的所有进一步使用都将失败。

First, turn on compiler warnings. It may help you find some things that you think are OK but which really aren't. For example, functions with non-void return types should always return something. If they don't, then your program's behavior is undefined, and undefined behavior includes "working exactly as you wanted, except for some subtle difference later in the program." If you're using g++, the option for warnings is -Wall.

Second, note that it's not just the highlighted line that isn't running. The entire pushSynonyms function never gets called. Has your class covered how to use the debugger yet? If so, then consider using it. If not, then try putting some "cout" statements in your program so you can see exactly how far your program gets before it goes wrong.

Third, note that when a stream read failure occurs, the stream's fail bit is set. Until you clear it (as shown by sth's answer), no further extraction can occur from that stream, so all further uses of >> and getline will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文