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. C#阅读文本文件
- 2. 阅读文本文件
- 3. jquery - 阅读文本文件?
- 4. 文本文件阅读
- 5. 。当阅读文本文件
- 6. 阅读和文本文件
- 7. 阅读文本文件 - C++
- 8. 阅读文本文件
- 9. Perl阅读文本文件
- 10. Android阅读文本文件
- 11. 阅读文本文件
- 12. 阅读文本文件
- 13. 阅读重文本文件
- 14. C阅读文本文件
- 15. 阅读文本文件C#
- 16. 阅读文本文件
- 17. iPhone文本文件阅读
- 18. 阅读文本文件
- 19. 阅读文本文件C
- 20. 阅读文本文件
- 21. 阅读大文本文件
- 22. 阅读文本文件
- 23. 阅读从文本文件
- 24. 阅读文本文件
- 25. 阅读文本文件
- 26. 阅读文本文件
- 27. 阅读文本文件
- 28. PyQT阅读文本文件
- 29. Highcharts阅读文本文件
- 30. 阅读文本文件
我认为你应该指定操作系统,因为它的操作系统具体如何快速读取。例如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