【问题标题】:Finding the longest word in a file查找文件中最长的单词
【发布时间】:2017-10-02 19:48:37
【问题描述】:

大家好,我正在尝试编写一个程序,它可以在我们的 ifstream 文件中找到最长的行和单词。我有程序来查找最长的行,但在查找最长的单词时遇到了麻烦。这就是我目前所拥有的。

这是我得到的错误。

program1.cpp:44:30: error: ‘get’ was not declared in this scope                                                                   
          while (get(file,word))  

据我所知,getline 用于获取实际行,而 get 应该获取该行中的字符。

#include <iostream>
#include<stdlib.h>
#include<getopt.h>
#include"stdio.h"
#include<fstream>
//#include<sstream>

using namespace std;

int main(int argc, char *argv[]) {

   size_t longest = 0;
   string longestWord;
   int Lcount = 0;
   int Wcount = 0;
   int option;
   string line;
   string word;

    if(argc <= 1)
    {
       cout << "NO FILES\n";
       return 0;
    }
   else
   {

   for(int i = 1;i<argc;i++){
      ifstream file (argv[i]);
      if (!file.is_open() ){
      cout << argv[i] << " FILE NOT FOUND\n"; // watch out for /n
      }
     //else if(!file.close()){
        // cout << argv[i] << "NO FILES\n"; }
      else{
         while (getline(file,line))// Length of Longest Line
         {
            if(line.size() > longest){
               longest = line.size(); 
         }
            else if(line.size() == longest){// Number of lines with longest length
                     ++Lcount;}
         }
         while (get(file,word))
         {
            if(word.size() > longestWord.size()){
               longestWord = word; 
         }
            else if(word.size() == longestWord.size()){
                     ++Wcount;}
         }

   }
   }
   }

     while ((option = getopt (argc, argv, "c:")) != -1){
        switch (option)
        {
           case 'c': 
              {
              for(int i = 1;i<argc;i++){
                  ifstream file (argv[i]);

              cout << argv[i] << "\n";
              cout << longestWord << " (" << Wcount << ")" << "\n";
              cout << longest << " (" << Lcount << ")" << "\n";
                 break;
              }
              }

                 default: 
                 //cout << "UNRECOGNIZED FLAG\n";
              if(option != 'c'){
                 cout << "UNRECOGNIZED FLAG\n";
                 return 0;}

        }
     }

        //if (option != 'c')
           //cout << "UNRECOGNIZED FLAG\n";

   return 0;
}

【问题讨论】:

  • 请发布您的get(file, word) 方法的内容。
  • 我打算使用 ifstream
  • 什么是line-英文句子?或“到下一个'\n'”
  • 什么是单词(或者,什么子字符串特征可能不是“单词”)?也许单词没有冒号、下划线、逗号、数字,不能以数字开头。或者一个词可能是两个空格之间的任何字符串?

标签: c++ file text get getline


【解决方案1】:

在您包含的库中没有与您的 get 功能等效的功能。尝试使用文件>>单词而不是获取。同样 ifstream 再次使用不同的变量来获取单词。参考http://www.cplusplus.com/forum/general/34420/

【讨论】:

  • 嗨,谢谢你的回复,当我把它改成这个时:while (file>>word(file,word)){我得到了这个错误
  • 错误:不匹配调用 '(std::__cxx11::string {aka std::__cxx11::basic_string}) (std::ifstream&, st d::__cxx11: :string&)' while (file>>word(file,word))
  • 把它改成文件>>word
  • 我这样做了,它确实可以编译,但我遇到的问题是我希望循环获取单词中的字符,类似于 getline 如何获取文件中的行。
  • 添加 ifstream 文件 (argv[i]); if (!file.is_open() ){ cout >word) 上方的 /n }。 getline 将 ifstream 带到最后。我们只需要重置它。
猜你喜欢
  • 2013-04-28
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2022-01-03
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多