[英]Read till end of a boost memory mapped file in VC++
I am writing a program in C++ using VS2010 to read a text file and extract certain information from it. 我正在使用VS2010编写一个C ++程序来读取文本文件并从中提取某些信息。 I completed the code using filestream and it worked well. 我使用filestream完成了代码,效果很好。 However now I am asked to map the file to memory and use it rather than the file operations. 但是现在我被要求将文件映射到内存并使用它而不是文件操作。
I am absolutely a newbie in case of memory mapping. 在内存映射的情况下,我绝对是一个新手。 A part of the code I have written is as follows. 我写的代码的一部分如下。
boost::iostreams::mapped_file_source apifile;
apifile.open(LogFileName,LogFileSize);
if(!apifile.is_open())
return FILE_OPEN_ERROR;
// Get pointer to the data.
PBYTE Buffer = (PBYTE)apifile.data();
while(//read till end of the file)
{
// read a line and check if it contains a specific word
}
While using filestream I would have used eof
and getline
and string::find
for performing the operations. 在使用文件流时,我会使用eof
和getline
以及string::find
来执行操作。 But I don't have any idea on how to do it using memory mapped file. 但我不知道如何使用内存映射文件来做到这一点。
EDIT 1: 编辑1:
int ProcessLogFile(string file_name)
{
LogFileName = file_name;
apifile.open(LogFileName);//boost::iostreams::mapped_file_source apifile(declared globally)
streamReader.open(apifile, std::ios::binary);//boost::iostreams::stream <boost::iostreams::mapped_file_source> streamReader(declared globally)
streamoff Curr_Offset = 0;
string read_line;
int session_id = 0;
int device_id = 0;
while(!streamReader.eof())
{
\\COLLECT OFFSETS OF DIFFERENT SESSIONS
}
streamReader.close();
}
This function worked and i got the offsets to the required structure. 这个功能起作用了,我得到了所需结构的偏移量。
Now after calling this function, I call yet another function as follows: 现在调用此函数后,我调用另一个函数如下:
int GetSystemDetails()
{
streamReader.open(apifile, std::ios::binary);
string read_line;
getline(streamReader,read_line);
cout << "LINE : " << read_line;
streamReader.close();
}
I don't get any data in read_line. 我没有在read_line中获得任何数据。 Is that memory mapping only for a single function? 那个内存映射只针对单个函数吗? How can I use the same memory mapped file across different functions? 如何在不同的函数中使用相同的内存映射文件?
I agree with people questioning the use of a mmap if you just read through the file sequentially. 如果您只是按顺序读取文件,我同意人们质疑mmap的使用。
boost::mapped_file_source
models a Device. boost::mapped_file_source
为设备建模。 There's two approaches to use such a Device: 有两种方法可以使用这样的设备:
data()
as you try) 原始使用它(在尝试时使用data()
) You can use the mapped_file_source to report the actual size ( m.data()+m.size()
). 您可以使用mapped_file_source报告实际大小( m.data()+m.size()
)。
Let's take a sample to count lines: 我们来取一个样本来计算行数:
#include <boost/iostreams/device/mapped_file.hpp> // for mmap
#include <algorithm> // for std::find
#include <iostream> // for std::cout
#include <cstring>
int main()
{
boost::iostreams::mapped_file mmap("input.txt", boost::iostreams::mapped_file::readonly);
auto f = mmap.const_data();
auto l = f + mmap.size();
uintmax_t m_numLines = 0;
while (f && f!=l)
if ((f = static_cast<const char*>(memchr(f, '\n', l-f))))
m_numLines++, f++;
std::cout << "m_numLines = " << m_numLines << "\n";
}
You could possibly adapt this. 你可以适应这个。 I have several more complicated parsing examples based on memory mapped files: 我有几个基于内存映射文件的更复杂的解析示例:
open()
+ read()
was faster than the memory map due the sequential access nature 请注意,在更新中您可以看到,由于顺序访问性质, open()
+ read()
确实比内存映射更快 istream
2.将源设备包装在istream
This gives you all the usual stream-based operations of c++ standard streams, so you can detect the end of the file like you would always: 这为您提供了所有常见的基于流的c ++标准流操作,因此您可以像往常一样检测文件的结尾:
#include <boost/iostreams/device/mapped_file.hpp> // for mmap
#include <boost/iostreams/stream.hpp> // for stream
#include <algorithm> // for std::find
#include <iostream> // for std::cout
#include <cstring>
int main()
{
using boost::iostreams::mapped_file_source;
using boost::iostreams::stream;
mapped_file_source mmap("test.cpp");
stream<mapped_file_source> is(mmap, std::ios::binary);
std::string line;
uintmax_t m_numLines = 0;
while (std::getline(is, line))
{
m_numLines++;
}
std::cout << "m_numLines = " << m_numLines << "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.