2014-03-31 59 views
0

我讀一個字一個文本文件,字和我試圖找到這一行字是在數量例如,如果我有以下幾點:C++如何從文本文件中查找單詞的每一行?

Dog Cat 
Car 
Truck 

狗是在一行中發現,貓

int main(){ 
string word; 
ifstream inFile; 
Node* rootPtr = NULL; // Pointer to the root node 

inFile.open("example.txt"); 
if (!inFile) 
    cout << "Unable to open text file"; 

while (inFile >> word) { 
    if (word == "#") 
     break; 

    /* THIS DOES NOT WORK! Most likely because my text file doesn't contain /n but this is the 
    kind of logic I am looking for 
    else if (word == "/n"){ 
     counter++; 
     cout << counter; 
    } 
    */ 

    else{ 
    rootPtr = Insert(rootPtr,word.substr(0,10)); 
    } 
} 
inOrderPrint(rootPtr); 
inFile.close(); 
} 

可以忽略任何與指針:第一線上,租車行兩個,卡車線3

我有下面的代碼被發現。這是其他一些東西。我試圖找出如何檢查行的末尾,並創建一個計數器,每增加一行就會結束,但我沒有成功。

感謝您的幫助!

+0

你必須自己追蹤行號。你能告訴我們你的失敗嘗試嗎? – Cameron

+0

感謝卡梅隆,我編輯我的帖子,以顯示我曾嘗試過。 – Nick

+1

/n你的意思是'\ n'? – Lefsler

回答

0

可以使用getline函數

string line; 
int lineNum = 0; // Or 1 
while(getline(infile, line)) 
{ 
    i++; 
} 

如果你想通過文字分割線可以使用stringstream的。

#include <sstream> 
// Your code 
while(getline(infile, line)) 
{ 
    stringstream ssLine(line); 
    string substr; 
    while(ssLine) 
    { 
     ssLine >> substr; 
     // substr will now hold each word (words should be separated by spaces) 
    } 
    i++; 
} 

或者更好的,我有我的版本分裂,並歡迎您使用

/** 
* Equivalent to java's string.split() function. 
* 
* @param toPopulate The return value of this function. 
* @param s   The string we want to split. 
* @param delim  The delim which we want to split. This will not be included in 
*     the splitted string. User should pass only one character to this 
*     string. 
*/ 
void split(vector<string> &toPopulate, string s, string delim) 
{ 
// Will hold the start of the substring (after the delim). Initially the 
// substring will start at 0. 
int substrStart = 0; 
while (substrStart < s.length()) 
{ 
    // Will hold the position of the delim. 
    int curFoundPos = s.find(delim, substrStart); 
    // Holds the current substring. 
    string oneOfSplittedStr; 

    // The delim not found. So, take the substring from previous delim to end. 
    if (curFoundPos == -1) 
    { 
     oneOfSplittedStr = 
      s.substr(substrStart, s.length() - substrStart); 
     // To break off the loop. If not for this stmt, we will go into infinite loop. 
     substrStart = s.length(); 
    } 
    else 
    { 
     oneOfSplittedStr = 
      s.substr(substrStart, curFoundPos - substrStart); 
     // our next substring will start one greater than the current found position. 
     substrStart = curFoundPos + 1; 
    } 
    // Empty - Nah 
    if (!oneOfSplittedStr.empty() && oneOfSplittedStr.compare("") != 0) 
     toPopulate.push_back(oneOfSplittedStr); 
} 

}

而且你可以隨時使用boost's split

0

你可以嘗試一些的東西。一個地圖會工作:

#include <map> 
map <int,string> words; 

然後同時加入的話:

int wordNum = 0; 
while (inFile >> word) { 
    if (word == "#") 
     break; 
    else{ 
     words[wordNum] = word; 
    } 

和召回你的話是這樣的:

int x = 0; 
while (x < map.size()) { 
    cout << words[x] << " "; 
    x++; 
} 

另一種選擇是你的話存儲到包含一個struct兩個字符串或任意兩個相應的數據類型(一個用於您的單詞,一個用於#):

struct words_struct{ 
    string words; 
    string wordNum; 
} store ; // single instance 

和商店的話是這樣的:

int x=0; 
while (inFile >> word) { 
    if (word == "#") 
     break; 
    else{ 
     store->words.append(word); 
     store->wordNum.append(x); 
     x++; 
    } 

上面的代碼需要一些fixin'(即,內部 - >串等之間沒有空格)但要點是正確的。希望這可以幫助!祝你好運!

+0

謝謝約書亞!我正在嘗試你的映射方法。 (x Nick

0

您的要求存在問題:如果單詞存在多行,該怎麼辦?

對於簡單的情況下,我建議保持一個行計數器,並使用std::map<string, unsigned int>,其中是你的話,並unsigned int類型是第一次出現的行號。

要處理某個單詞出現的所有行號,您可能需要使用std::map<string, std::vector<unsigned int> >,其中std::vector包含該單詞出現的所有行號。

例子:

typedef std::map<std::string, unsigned int> Word_Ref_Container; 
Word_Ref_Container word_line_reference; 
//... 
std::string text_line; 
unsigned int line_number = 1; 
while (getline(input_file, text_line) 
{ 
    std::istringstream text_stream(text_line); 
    std::string word; 
    while (text_stream >> word) 
    { 
    if (word_line_reference.find(word) != word_line_reference.end()) 
    { 
     word_line_reference[word] = line_number; 
    } 
    } 
    ++line_number; 
} 
0

我找到了一個很好的方式去這樣做。我只需要添加以下內容:

char c; 
    while (inFile >> word) { 
     c = inFile.get(); 
     else if (c=='\n'){ 
      rootPtr = Insert(rootPtr,word.substr(0,10)); 
      counter++; 
     } 
    } 

感謝您的所有建議!

相關問題