2012-05-13 77 views
0

我对getopt的理解非常有限。 但我知道argv[0]是exe文件,argv[1]是选项,argv[2]是要比较的字,argv[3]是我要搜索的字典或文档(文件.txt)。单词比较 - 矢量/ Getopt

我试图设置一个指向字典的指针,然后遍历它以查看是否存在匹配的文本文件argv[2](输入字),并且匹配输出argv[2]字。 下面是我当前的代码有错误:

main.cpp:61: error: no match for 'operator==' in 'list == *(argv + 12u)' 
main.cpp:64: error: no match for 'operator*' in '*list' 

任何帮助将不胜感激。

#include <cstdlib> 
#include <unistd.h> 
#include <vector> 
#include <iostream> 
#include <string> 
#include <iterator> 
using namespace std; 

int main(int argc, char** argv) { 
    enum { 
     WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED 
    } mode = WHOLE; 
    bool jumble = false; 
    bool ignore_case = false; 
    bool invert = false; 
    string length = "0,0"; 
    int c; 
    string input; 
    vector <string> list; 
    vector <string>::iterator i; 

    while ((c = getopt(argc, argv, ":wpsaejivn:")) != -1) { 
     switch (c) { 
      case 'w': mode = WHOLE; 
       break; 
      case 'p': mode = PREFIX; 
       break; 
      case 's': mode = SUFFIX; 
       break; 
      case 'a': mode = ANYWHERE; 
       break; 
      case 'e': mode = EMBEDDED; 
       break; 
      case 'j': jumble = true; 
       break; 
      case 'i': ignore_case = true; 
       break; 
      case 'v': invert = true; 
       break; 
      case 'n': length = optarg; 
       break; 
      default: WHOLE; 
       break; 
     } 
    } 
    argc -= optind; 
    argv += optind; 

    switch (mode) { 
     case WHOLE: 
      while(argc != -1){ 
       list == argv[3]; 
       for(i == list.begin(); i != list.end(); i++) 
       if(argv[1] == argv[3]){ 
        cout << *list << endl; 
       }else cout << "Did not work again" << endl; 
      }         
    } 
    return 0; 
} 
+1

没有进入逻辑,什么代码是干什么的,这两个问题,我的代码(这是由编译器报告两个问题),请参阅:1.'名单== argv [3]'。 'list'是一个'vector',你将它与'const char *'进行比较。 2.'cout << * list << endl'。你在这里指“*我”吗? 'std :: vector'没有定义'operator *'。 – Vikas

+0

另一个问题:'while(argc!= -1)'。这不会在循环内减少'argc',将导致无限循环。 – Vikas

+0

这个想法是比较arg [2](输入字)和arg [3](.txt文件),我只注意到if(argv [1] == argv [3])应该是if(argv [2 ] == argv [3])。 – user1340113

回答

2

如果我理解正确,那么在这里你不需要一个向量。您需要阅读argv [3]文件,逐字解析并在找到等于argv [2]的单词时停止。

我像你想要的东西,如:

#include <string> 
#include <fstream> 
#include <ostream> 

using namespace std; 

int main(int argc, char** argv) 
{  
    // The part where you parse the input and validate it 
    // ... 

    // Read the dictionary specified in argv[3] and compare it with argv[2] line by line 
    ifstream input_file(argv[3]); 
    string match_string(argv[2]); 
    string current_string; 
    bool found = false; 
    while(getline(input_file, current_string) && !found) 
     found = (current_string.compare(match_string) == 0); 

    // Check the result 
    if (found) 
    cout << "Found " << match_string << " in " << argv[3] << endl; 
    else 
    cout << "Did not work again" << endl; 

    return 0; 
} 

在这个基本解决方案,我认为在字典文件中的每个字是一个独立的行。当然,您需要根据需要对其进行修改,并根据需要添加更多输入验证。

希望它有帮助!

1

没有进入getopt,我不认为你的问题在这里,我会回答给出一个文件列表的单词和一个单词的问题,你如何确定该单词是否存在于文件中。

可以有很多方法可以做到这一点,但在概念上它涉及以下步骤:

  1. 读输入文件,并创建一个字典出来。
  2. 匹配字典中的输入单词。

代码片段:

#include <fstream> 
#include <iostream> 
#include <iterator> 
#include <string> 
#include <set> 

int main (int argc, char *argv[]) 
{ 
    // Input file stream 
    std::ifstream file_in(argv[3]); 

    // Iterators to iterate over input file 
    std::istream_iterator<std::string> in_begin(file_in), in_end; 

    // Create a dictionary of words. 
    // Using std::set for this. 
    std::set<std::string> dictionary(in_begin, in_end); 

    // Word to find in dictionary 
    std::string word(argv[2]); 

    // See if the word is present in our dictionary 
    if(dictionary.find(word) != dictionary.end()) 
    std::cout << word << " found in " << argv[3] << std::endl; 
    else 
    std::cout << word << " not found in " << argv[3] << std::endl; 
} 
+0

代码块的第一行是'#include '。这是什么解释为别的东西。我正在尝试修复它。 – Vikas

+0

@ssjeitan,是啊我试图解决这个'#include'的事情:-) – Vikas

+0

你的代码有错误,argv [2]是要匹配的词,而不是字典名... –