[英]Read a file line by line with mmap
I have a program that reads a file line by line whose size varies, i would like use mmap but how use it to read a file line by line? 我有一个程序可以逐行读取大小不一的文件,我想使用mmap,但如何使用它逐行读取文件?
Thank you for your answers! 谢谢您的回答!
Once you have mmap()
ed the file, you can make the file available to a suitable stream buffer reading data from existing memory and then use std::getline()
: 一旦有了
mmap()
ed文件,就可以使该文件可用于合适的流缓冲区,以从现有内存中读取数据,然后使用std::getline()
:
#include <streambuf>
#include <string>
#include <istream>
struct membuf
std::streambuf {
membuf(char* start, size_t size) {
this->setg(start, start, start + size);
}
};
int main() {
// mmap() the file yielding start and size
membuf sbuf(start, size);
std:istream in(&sbuf);
for (std::string line; std::getline(in, line); ) {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.