我想要查找一些單詞後,我得到整個文件char *。我知道如何使用字符串類的功能,但我不想再次將數據複製到字符串變量。是否有任何類似的函數可用於char *字符串或應該仍然使用字符串類?搜索LPSTR字符串
回答
http://www.cplusplus.com/reference/clibrary/cstring/strstr/
基本字符串的字符串處理功能位於:字符串/ string.h中報頭: http://www.cplusplus.com/reference/clibrary/cstring/
可以使用strstr
(爲一個例子)進行搜索。如果數據足夠大以至複製時間很長,那麼使用Boyer-Moore-Horspool搜索等方法可能會值得。
既然你知道如何使用它,爲什麼不先把文件加載到一個字符串呢?
#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;
}
而不是strstr(str,「find」)? – 2010-03-31 20:51:21
而不是'獲取整個文件char *'&strstr和David'知道如何使用字符串類函數'(並且因爲它被標記爲C++) – 2010-03-31 21:07:19
您可能需要使用超過strstr()
,因爲你可能會感興趣的單詞邊界(I;猜,你需要找到由空格或開始/結束邊界匹配線)。
各種標準庫函數可能會感興趣:
strstr()
strtok() (or it's non-standard but sill useful cousin strtok_r())
strspn()
strpbrk()
strcspn()
strchr()
但是,字符串分析是在傳統的C真正的痛苦(在我的經驗),你可能想看看在現有的解析庫(或正則表達式庫)可能有助於緩解疼痛。
- 1. 搜索字符串
- 2. 字符串搜索
- 3. 搜索字符串
- 4. 搜索字符串
- 5. 搜索字符串
- 6. 搜索字符串
- 7. 字符串搜索
- 8. 搜索字符串
- 9. 搜索字符串
- 10. 搜索字符串
- 11. 字符串搜索
- 12. 搜索字符串
- 13. C++ LPSTR到C#字符串.NET包裝
- 14. sed命令來搜索字符串和搜索的字符串
- 15. 變量搜索,返回「字符串,字符串2」上搜索
- 16. 搜索字符串中的字符串
- 17. 搜索字符串內的字符串
- 18. 數字搜索字符串
- 19. 搜索字符串文字
- 20. vb.net搜索搜索字符串
- 21. 塗改搜索字符串搜索
- 22. MySQL搜索字符串
- 23. 搜索字符串集合
- 24. 通過搜索字符串
- 25. 查詢字符串搜索
- 26. 字符串搜索數組
- 27. ArrayList字符串搜索
- 28. 搜索Json字符串
- 29. 搜索循環字符串
- 30. 字符串搜索到HTML
請不要簡單地發佈一個鏈接作爲答案。至少提供一個句子來解釋它所鏈接的內容。 – 2010-03-31 19:51:59
你是對的,但文檔應該是自我解釋。指向解決方案的指針應該足夠了。 – 2010-03-31 19:57:54