我正在使用VS2010编写一个C++程序来读取文本文件并从中提取某些信息.我使用filestream完成了代码,效果很好.但是现在我被要求将文件映射到内存并使用它而不是文件操作.
在内存映射的情况下,我绝对是一个新手.我写的代码的一部分如下.
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
}
Run Code Online (Sandbox Code Playgroud)
在使用FILESTREAM我会用eof与getline和string::find用于执行操作.但我不知道如何使用内存映射文件来做到这一点.
编辑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 …Run Code Online (Sandbox Code Playgroud) <spanstream>将在 C++23 中首次亮相(请参阅cppreference)。根据提案,它们是std::span基于缓冲区的字符串流。
我的问题是:
std::spanstream与旧的std::strstream(或strstream在 C++ 98 中已弃用)有些等效?