Efficiently read a portion of text in C++

2019-07-15 14:14发布

问题:

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



标签: c++ boost text