Read to the end of the file with memory formatting in VC ++

I am writing a C ++ program using VS2010 to read a text file and extract certain information from it. I completed the code with a filter and it worked well. However, now I will be asked to map the file to memory and use it, rather than file operations.

I am absolutely new to memory mapping. Part of the code I wrote 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 } 

When using filestream, I would use eof and getline and string::find to perform operations. But I do not know how to do this using a memory mapped file.

EDIT 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 offsets in the required structure.

Now, after calling this function, I call 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 do not receive any data in read_line. Is this memory mapping for only one function? How can I use the same file with memory mapping in different functions?

+6
source share
1 answer

I agree that people are asking for the use of mmap if you just read the file sequentially.

boost::mapped_file_source simulates a device. There are two approaches to using such a device:

  • use it raw (using data() when you try)
  • using flow fairing

1. Using the source device source

You can use the mapped_file_source file to represent the actual size ( m.data()+m.size() ).

Let's take a sample to count the 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', lf)))) m_numLines++, f++; std::cout << "m_numLines = " << m_numLines << "\n"; } 

You could adapt this. I have a few more complicated examples of parsing based on memory mapped files:


2. Wrapping the source device in istream

This gives you all the normal stream operations of standard C ++ streams, so you can detect the end of the file, as always:

 #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"; } 
+11
source

Source: https://habr.com/ru/post/976410/


All Articles