Q
閱讀文本文件
0
A
回答
0
流類(ifstream)實際上做得很好;假設你沒有限制,否則請確保關閉sync_with_stdio(在ios_base::)。您可以使用getline()直接讀入std :: strings,但從性能角度來看,使用固定緩衝區作爲char *(chars或old-school char []的向量)可能會更快(風險更高/更復雜)。
如果你願意玩頁面大小計算等遊戲,你可以去mmap路線。我可能首先使用流類來構建它,看看它是否足夠好。
根據您對每行數據所做的操作,您可能會開始發現處理例程是優化點而不是I/O。
0
使用舊樣式文件io。
fopen the file for binary read
fseek to the end of the file
ftell to find out how many bytes are in the file.
malloc a chunk of memory to hold all of the bytes + 1
set the extra byte at the end of the buffer to NUL.
fread the entire file into memory.
create a vector of const char *
push_back the address of the first byte into the vector.
repeatedly
strstr - search the memory block for the carriage control character(s).
put a NUL at the found position
move past the carriage control characters
push_back that address into the vector
until all of the text in the buffer has been processed.
----------------
use the vector to find the strings,
and process as needed.
when done, delete the memory block
and the vector should self-destruct.
0
如果使用文本文件存儲整數,浮點數和小弦,我的經驗是FILE
,fopen
,fscanf
已經足夠快,你也可以直接得到的數字。我認爲內存映射是最快的,但它需要你編寫代碼來解析文件,這需要額外的工作。
相關問題
- 1. 閱讀文本文件
- 2. jquery - 閱讀文本文件?
- 3. 閱讀文本文件
- 4. 閱讀文本文件
- 5. 閱讀文本文件
- 6. 閱讀文本文件
- 7. Android閱讀文本文件
- 8. 閱讀文本文件
- 9. 閱讀文本文件,matlab
- 10. 文本文件閱讀
- 11. 閱讀文本文件
- 12. 閱讀文本文件
- 13. 閱讀文本文件
- 14. 閱讀文本文件
- 15. 閱讀文本文件
- 16. 閱讀和文本文件
- 17. Perl閱讀文本文件
- 18. C閱讀文本文件
- 19. 閱讀文本
- 20. 閱讀本地文件
- 21. 閱讀Bash腳本文件
- 22. 閱讀/寫作 - 文本文件或PostGres,請閱讀更多
- 23. 閱讀文件
- 24. 閱讀文件
- 25. 如何閱讀遠程文本文件
- 26. 閱讀文本分隔文件C
- 27. 閱讀一個文本文件android
- 28. 閱讀WP7中的文本文件
- 29. 閱讀ms訪問文本文件
- 30. 閱讀並從文本文件C++
我認爲你應該指定操作系統,因爲它的操作系統具體如何快速讀取。例如Windows允許內存映射文件 – 2010-01-18 02:41:17
答案也取決於你打算如何處理文本。 Unix也有內存映射文件。 – Omnifarious 2010-01-18 02:54:19
如果你沒有做家庭作業或者做一個需要C++的項目,那麼不要在Linux中重新發明輪子,有很多工具(用C/C++完成)讀取文件,例如grep,awk等。如果你仍然想在C/C++中做到這一點,你可以檢查他們的來源,看看它是如何完成的。 – ghostdog74 2010-01-18 02:56:44