Efficiently read a portion of text in C++

纵然是瞬间 提交于 2020-01-24 13:12:07

问题


I've read this and then this questions about how to efficiently read big amount of text (floats in the second question) in C++ exploiting the boost::spirit library.

From what I've seen, the solutions proposed in the questions above read the whole text, while I need to read a portion of the input text (for example from char x to char y).

Can I exploit the library above for this purpose? How could I efficiently do it otherwise?


回答1:


You don't even need to map a subsection of the file, because mmap just virtually maps memory blocks. Actual pages are only loaded on demand, so you could map the full 12GiB of a file even if you have only, say, 4GiB of physical RAM (not even requiring swap).

If your file is text-bases, you will want to find the start-of-line from a random location in the file.

An example of something similar is in the second approach here: Using boost::iostreams::mapped_file_source with std::multimap



来源:https://stackoverflow.com/questions/34394635/efficiently-read-a-portion-of-text-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!