0

I'm a newbie to the world of Boost and was trying to Read/Write in memory mapped binary files. But I don't really know what to do actually and stuck somewhere in between. To be precise, I want my binary file to be memory mapped and then perform read/write operations on it. Till now, I've been trying to do stuff like :

stream_buffer<file_sink> is("test.txt");
std::ostream out(&is);
out << "This is written to file." << std::endl;
is.close();

Please help me understand how to proceed for this kind of problem. A POC would be great to understand. Thanks

naiveCoder
  • 1
  • 1
  • 3

1 Answers1

1

You can check the example from the book "Boost C++ Application Development Cookbook" available on GitHub

Sergei Nikulov
  • 5,029
  • 23
  • 36
  • I've already gone through the doc part, still not able to get few things. See what i want is to map my file to memory, perform read / write operations on it and the changes should be visible on my file stored on disk. file_mapping fm(argv[1], mode); mapped_region region(fm, mode, 0, 0); void* add = region.get_address(); char* begin = static_cast(add); cout << "Mapped region size : " << region.get_size(); string mystr("Voila ! Good Morning. In new line. Hello. "); cout << endl << mystr.size() << endl; memcpy(begin, mystr.c_str(), mystr.size() + 1); – naiveCoder Aug 10 '16 at 12:23
  • But the problem is that mapped_region is a fixed memory. But my string can be of any length. So i want something that doesn't need to depend on size or i can alter the size according to length of my string. – naiveCoder Aug 10 '16 at 12:29
  • @naiveCoder usually you unable to change size except by re-mapping http://stackoverflow.com/questions/6096485/how-to-dynamically-expand-a-memory-mapped-file Keep in mind that it's not exactly the file - it virtual memory blocks, so do not expect file behaviour. – Sergei Nikulov Aug 10 '16 at 13:56