2010-03-31 47 views
0

我想要查找一些單詞後,我得到整個文件char *。我知道如何使用字符串類的功能,但我不想再次將數據複製到字符串變量。是否有任何類似的函數可用於char *字符串或應該仍然使用字符串類?搜索LPSTR字符串

回答

1

可以使用strstr(爲一個例子)進行搜索。如果數據足夠大以至複製時間很長,那麼使用Boyer-Moore-Horspool搜索等方法可能會值得。

0

既然你知道如何使用它,爲什麼不先把文件加載到一個字符串呢?

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

int main(int argc, char* argv[]){ 
std::ifstream ifsFile(<file_name>); 
ifsFile.unsetf(std::ios_base::skipws); // make sure we're not skipping whitespaces 

std::istream_iterator<char> begin(ifsFile), end; 
std::string strFile(begin, end); // load the file into the string 

// now print the file/search for words/whatever 
std::copy(strFile.begin(), strFile.end(), std::ostream_iterator<char>(std::cout, "")); 

return 0; 
} 
+0

而不是strstr(str,「find」)? – 2010-03-31 20:51:21

+0

而不是'獲取整個文件char *'&strstr和David'知道如何使用字符串類函數'(並且因爲它被標記爲C++) – 2010-03-31 21:07:19

0

您可能需要使用超過strstr(),因爲你可能會感興趣的單詞邊界(I;猜,你需要找到由空格或開始/結束邊界匹配線)。

各種標準庫函數可能會感興趣:

strstr() 
strtok() (or it's non-standard but sill useful cousin strtok_r()) 
strspn() 
strpbrk() 
strcspn() 
strchr() 

但是,字符串分析是在傳統的C真正的痛苦(在我的經驗),你可能想看看在現有的解析庫(或正則表達式庫)可能有助於緩解疼痛。