2016-02-29 67 views
0

我是C++的新手,正在嘗試在文件中對2個字(雙精度和三精度)進行字符串搜索並打印找到它們的行。有些行只有「double」這個詞,有些只有「triple」這個詞。逐行讀取文件並輸出第一個顏色

該代碼似乎不會輸出單詞,它只是在循環結尾處打印最後一行。

我忘了補充說我需要打印找到該單詞的行的第一個元素,我可以找到找到該單詞的行,但是,它似乎不打印第一個元素文件。

這是我的代碼。

int main(int argv, char *argc[]){ 
    const string filen("test.txt"); 
    ifstream inFile(filen.c_str()); 
    string line = ""; 

    char IDList[10]; 
    string ID = ""; 
    char* double_word = "double"; // test variable to search in file 
    char* triple_word = "triple"; 
    stringstream ss; 

    string word = ""; 
    unsigned int currentLine = 0; 

    // iterate through each line and check if the words double or triple exist. 

    while(getline(inFile, line)){ 
      currentLine++; 
      if (line.find(double_word) != string::npos) { 

        cout << "found the word \"double\" on line: " << currentLine << endl; 
    // this part takes the input file and reads the first character of the line i.e. the ID and adds it to the IDList 
    // string array. 
        while(inFile >> IDList){ 
          cout << "File Id: " << IDList << endl; 
          inFile.ignore(numeric_limits<streamsize>::max(), ' '); 

          for(int i=0;i<10;i++){ 
            ss << IDList[i] << endl; 
          } 
        word = ss.str(); 
        } 
      } 
      else if(line.find(triple_word) != string::npos){ 

        cout << "found the word \"triple\" on line: " << currentLine << endl; 

      // now take the id of this file and add it to a different queue. 

        while(inFile >> IDList){ 
          cout << "File Id: " << IDList << endl; 
          inFile.ignore(numeric_limits<streamsize>::max(), ' '); 

          for(int i=0;i<10;i++){ 
            ss << IDList[i] << endl; 
          } 
        word = ss.str(); 
        } 
      } 
      else if(line.find(double_word) && line.find(triple_word) != string::npos){ 

        cout << "Found both words double and triple in line: " << currentLine << endl; 

        while(inFile >> IDList){ 
          cout << "File Id: " << IDList << endl; 
          inFile.ignore(numeric_limits<streamsize>::max(), ' '); 

          for(int i=0;i<10;i++){ 
            ss << IDList[i] << endl; 
          } 
        word = ss.str(); 
        } 
      } 
      else{ 
        cout << "neither word found, moving to next line" << endl; 
      } 
    } 

    inFile.close(); 
    cout << "Id's added to the queue" << word << endl; 
    return 0; 
} 
+0

你能請張貼輸入文件(至少一部分),你得到的輸出? –

+0

它只是格式文本文件:6431313 hello。一份雙層巧克力冰沙。 現在我的輸出只是:在第1行找到單詞「double」。 期望的輸出是:在行上找到雙字(lne數)。 Id是:6431313 –

回答

1

我認爲你可以簡化你的代碼,寫這樣的事:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <sstream> 

using std::string; 
using std::vector; 
using std::cout; 
using std::cin; 

int main(int argc, char* argv[]) { 
    const string filen("test.txt"); 
    std::ifstream inFile(filen.c_str()); 
    string line; 

    string double_word = "double"; // test variable to search in file 
    string triple_word = "triple"; 

    vector<string> IDs; 
    unsigned int currentLine = 0; 

    // iterate through each line and check if the words double or triple exist. 

    while(getline(inFile, line)){ 
     currentLine++; 
     bool found_d_word = line.find(double_word) != string::npos; 
     bool found_t_word = line.find(triple_word) != string::npos; 
     if (found_d_word && !found_t_word) 
      cout << "found the word \"double\" on line: " << currentLine << '\n'; 
     if (found_t_word && !found_d_word) 
      cout << "found the word \"triple\" on line: " << currentLine << '\n'; 
     if (found_d_word && found_t_word) 
      cout << "Found both words double and triple in line: " << currentLine << '\n'; 
     if (found_d_word || found_t_word) { 

      std::istringstream ss{line}; 

      string ID; 
      ss >> ID; 
      cout << "File Id: " << ID << '\n'; 
      // my guess: store all the IDs in one vector 
      IDs.push_back(ID); 

     } else { 
      cout << "neither word found, moving to next line\n"; 
     } 
    } 
    inFile.close(); 
    return 0; 
} 

一個與你的代碼的問題是,你先讀一行輸入文件(用函數getline)把它放在一個字符串中,然後在字符串中找到單詞「double」或「triple」時,嘗試從文件中讀取ID,而應該從同一個字符串讀取它。

+0

這個工程。不過,我對C++很陌生。必須學習矢量和事物。非常感激。 –

0

試試這個,

#include "iostream" 
#include "string" 
#include "sstream" 
#include "fstream" 
using namespace std; 

int main() 
{ 
    stringstream y; string x; int lc=0, id; 
    ifstream fin("file.txt"); 
    while (getline(fin, x)) 
    { 
     ++lc; 
     y.str(x); 
     y >> id; 
     bool d=x.find("double")!=string::npos; 
     bool t=x.find("triple")!=string::npos; 
     if (d and t) 
      cout << "found words double and triple on line " << lc 
      << ", id is " << id << endl; 
     else if (d) 
      cout << "found word double on line " << lc 
      << ", id is " << id << endl; 
     else if (t) 
      cout << "found word triple on line " << lc 
      << ", id is " << id << endl;  
    } 
} 
+0

嗨,謝謝你,但它沒有照顧測試可能包含這兩行的部分。 –

+0

「有些行只有單詞」double「,有些只有」triple「單詞。」請在問題中提及它是否需要。 – anukul