mercredi 28 septembre 2016

Boost::interprocess file_handle_t and read

How can I read the content of boost::interprocess::file_handle_t?

We are currently prototyping with code found at scoped_lock doesn't work on file?. This demonstrates how to write but we would also like to read from the file. How can this be accomplished? Done the normal google search and looked at boost docs and header files without any luck. Appreciate any input.

#include "boost/format.hpp"
#include "boost/interprocess/detail/os_file_functions.hpp"

namespace ip = boost::interprocess;
namespace ipc = boost::interprocess::detail;

void fileLocking_withHandle()
{
  static const string filename = "fileLocking_withHandle.txt";  

  // Get file handle
  boost::interprocess::file_handle_t pFile = ipc::create_or_open_file(filename.c_str(), ip::read_write);
  if ((pFile == 0 || pFile == ipc::invalid_file()))
  {
    throw runtime_error(boost::str(boost::format("File Writer fail to open output file: %1%") % filename).c_str());
  }

  // Lock file
  ipc::acquire_file_lock(pFile);

  // Move writing pointer to the end of the file
  ipc::set_file_pointer(pFile, 0, ip::file_end);

  // Write in file
  ipc::write_file(pFile, (const void*)("bla"), 3);

  // Unlock file
  ipc::release_file_lock(pFile);

  // Close file
  ipc::close_file(pFile);
}

We are developing on Windows and Linux.

Aucun commentaire:

Enregistrer un commentaire