2012-12-15 21 views
1

好了,所以我簡單的項目應該來搜索特定字符串在一個.txt文件中找到的所有案件。案件事宜,如果這個詞在另一個詞中被發現則很重要。奇怪的加入 - 計數出現一個詞的次數在文件

(例如:如果單詞是 「該」:

有效的發現包括:

蘋果= 1;

的戲劇性= 2;

無效的發現包括:

第四象(th與e之間的空格)

蘋果(市值)

如果這個詞是在一條線上的文件發現,我應該打印出來的線一次。 如果沒有找到它,我不應該打印它。

因此,舉例來說,我的程序的一個運行應該輸出:

Searching for 'the' in file 'test.txt' 
2 : that they do not use permeates the [C++] language. Another example 
3 : will further illustrate this influence. Imagine that an integer 
5 : What bit value should be moved into the topmost position? If we 
6 : look at the machine level, architectural designers are divided on 
8 : the most significant bit position, while on other machines the sign 
9 : bit (which, in the case of a negative number, will be 1) is extended. 
10 : Either case can be simulated by the other, using software, by means 
# occurrences of 'the' = 13 

不幸的是,我越來越

Searching for 'the' in the file 'test.txt' 
2: that they do not use permeates the [C++] language. Another example 
3: will further illustrate this influence. Imagine that an integer 
5: What bit value should be moved into the topmost position? If we 
6: look at the machine level, architectural designers are divided on 
8: the most significant bit position, while on other machines the sign 
9: bit (which, in the case of a negative number, will be 1) is extended. 
10: Either case can be simulated by the other, using software, by means 
11: of a combination of tests and masks. 
12: 
# occurrences of 'the' = 15 

我不理解爲什麼它認爲它發現了一個「了」在線路11和12

這是我的代碼:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstring> 

using namespace std; 

int main(int argc, char* argv[]){ 
//a char pointer is a c-string 
//the array is just an array of char pointers 
//argv[0] = pointer to the word to search for 
//argv[1] = pointer to fileNames 

//includes program name @ 0, so three args 
if (argc == 3){ 

    int wordCounter = 0; 

    ifstream myFile(argv[2]); 

    if (!myFile){ 
     cout << "File '" << argv[2] << "' could not be opened" << endl; 
     return 1; 
    } 

    else { 
     //counts the number of lines in file 
     int counter = 0; 

     //holds the new line in the file 
     char line[100]; 

     //copies string into buffer that is length of word 
     const char * word = argv[1]; 

     //holds whether found word 
     bool found = false; 

     cout << "Searching for '" << word << "' in the file '" << argv[2] << "'" << endl; 

     //number of chars in a line 
     int numChar = 0; 

     //saves every line 
     while (!(myFile.getline(line, 100)).eof()) { 
      //starts every new new at not having found the word 
      found = false; 
      //read in new line, so increases line counter 
      counter ++; 
      numChar = 0; 

      //find length of line 
      for (int i = 0; line[i] != '\n' && i < 101; i++){ 
       numChar++; 
      } 

      //finds how many times the key word appears in one line 
      //checks up to a few before the end of the line for the word 
      if (numChar >= strlen(argv[1])){ 
       for (int i = 0; i < numChar - strlen(argv[1]); i++){ 

        //if the current line letter equals the first letter of the key word 
        if (line[i] == word[0]){ 

         //continue looking forward to see if the rest of it match 
         for (int j = 0; j < strlen(argv[1]); j++){ 

          //if word doesn't match break 
          if (word[j] != line [i+j]){ 
           break; 
          } 

          //if matches all the way to end, add counter 
          if(j == strlen(argv[1]) - 1){ 
           wordCounter++; 
           found = true; 
          } 
         }//end 2ndfor 
        } 
       }//end 1stfor 

       //if the key word has been found, print the line 
       if (found){ 
        cout << counter << ": " << line << endl; 
       } 
      } 
     }//endwhile 

     cout << "# occurrences of '" << word << "' = " << wordCounter << endl; 
     myFile.close(); 
    }//end else 
}//end if 
return 0; 
}//end main 
+1

Urg,你不允許使用'std :: string'嗎? –

+0

'std :: string' + while-loop使這個問題**瑣碎**。如果你的教師不讓你使用這樣的標準庫實體,他們正式脫離現實,他們已經贏得了他們無疑的頭銜。 – WhozCraig

回答

0

你的程序相信有在線路11 "the"和12的原因是

for (int i = 0; line[i] != '\n' && i < 101; i++) 

,你檢查換行(這是不是在緩衝區,順便說一句),但不能用於終止0。所以,你檢查整個100個字符 - 一個更實際,因爲你也檢查不存在line[100],並計算"the"期從之前的線遺留下來的。

for (int i = 0; i < 100 && line[i] != '\0' && line[i] != '\n'; i++) 

應該解決這個問題。

檢查索引第一個的有效性,以避免由於無效內存訪問而導致的未定義行爲。

+0

哇。謝謝。這是完全合理的。我現在感到很傻。 – katiea

0
  • getline(ifstream &,string)在閱讀行時會更有效,在某些情況下,該行可能會超過100個字符(這些字符包含空格)並使計數增加。此功能將讀取到的字符串,直到底線遇到
  • 你不遍歷文件正確,這可能會導致不確定的行爲,在這種情況下添加到您的奇怪的行數,一個正確的文件循環將是:

//program counts the number of lines in a file 
getline(myFile,line) //grab the line 
while(myFile) //while the filestream is open and reading 
{ 
    //manipulate line string 
    lineCount++; 
    getline(myFile,line) //re-read in next line 
} 
+0

如何檢查文件的結尾?我們需要.eof()檢查,對吧? – katiea

+0

ifstream持有一個與文件好壞有關的內部布爾值,只需寫'while(myFile)'將返回該布爾值以檢查文件輸入流是否仍然良好。 –

+0

添加此概念會導致從void *到char **的對話錯誤 - >我從未見過此錯誤。更改的代碼: getline(myFile,line); //保存每一行 while(myFile)//在沒有找到單詞 found = false時啓動每個新的新單元; //讀入新行,所以增加行計數器 counter ++; numChar = 0; 。 。 。 getline(myFile,line); } // end – katiea

相關問題