0

I am newbie to boost::iostream memory mapped file and I am having some difficulties in understanding the classes.

I would like my function to create a new memory map file for writing and reading. I was successful for the writing part but I don't know how to read back the values.

Reading the docs it looks like the mapped_file_params::mode param is ignored by both mapped_file_source and mapped_file_sink classes.

Possibly I would like to use it as it was a stream as I would like to use seekg and read.

If this is not possible what else can I use? Is it ok to use the mapped_file_sink::data() to read back?

Below my code

namespace bip = boost::iostreams;

bio::mapped_file_params prm("data.out");
prm.new_file_size = 256; // in reality it will be bigger.
prm.mode = std::ios::in | std::ios::out;
bio::stream<bio::mapped_file_sink> ooo;
ooo.open(bio::mapped_file_sink(prm));

char AA;

AA = 'A';
ooo.write(&AA,1);

AA = 'B';
ooo.write(&AA,1);  

char BB;
bio::seek(ooo,0,BOOST_IOS::beg);    
ooo.read(&BB,1); // this fails
cout << B << endl;
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173

1 Answers1

1

mapped_file_sink is write only - this is why it ignores the mode parameter. mapped_file_source is read only. To both read and write use mapped_file.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • Hi Wojtek. It works Thanks a lot! Now suppose I want to pass it to a stream I called the following but I am not 100% sure if it is correct: bio::stream bs(f); iostream& s(bs); where f is the bio::mapped_file. It looks as it is fine. Any issue with that approach? – Abruzzo Forte e Gentile Jan 29 '14 at 14:02
  • No issue - it should work, but normally you do not need `s` at all, use `bs` wherever you wanted to use `s`. – Wojtek Surowka Jan 29 '14 at 14:29