try
{
std::fstream openfile("example_file.txt");
openfile.exceptions(std::fstream::failbit | std::fstream::badbit);
openfile << "He";
openfile << "ll";
openfile << "o ";
openfile << "Wo";
openfile << "rl";
openfile << "d\n";
openfile.sync();
std::array<char, 12> buffer;
openfile.seekg(0, std::ios::beg);
openfile.read(buffer.data(), buffer.size());
openfile.close();
boost::afio::filesystem::remove("example_file.txt");
std::string contents(buffer.begin(), buffer.end());
std::cout << "Contents of file is '" << contents << "'" << std::endl;
}
catch(...)
{
std::cerr << boost::current_exception_diagnostic_information(true) << std::endl;
throw;
}
|
opens file
|
|
writes
|
|
syncs
|
|
reads
|
|
closes file
|
|
deletes file
|
|
namespace afio = BOOST_AFIO_V2_NAMESPACE;
namespace asio = BOOST_AFIO_V2_NAMESPACE::asio;
afio::current_dispatcher_guard h(afio::make_dispatcher().get());
afio::future<> openfile = afio::async_file(
"example_file.txt", afio::file_flags::create | afio::file_flags::read_write);
afio::future<> resizedfile = afio::async_truncate(openfile, 12);
std::vector<asio::const_buffer> buffers;
buffers.push_back(asio::const_buffer("He", 2));
buffers.push_back(asio::const_buffer("ll", 2));
buffers.push_back(asio::const_buffer("o ", 2));
buffers.push_back(asio::const_buffer("Wo", 2));
buffers.push_back(asio::const_buffer("rl", 2));
buffers.push_back(asio::const_buffer("d\n", 2));
afio::future<> written(afio::async_write(resizedfile, buffers, 0));
std::vector<std::string> buffers2 = {"He", "ll", "o ", "Wo", "rl", "d\n"};
afio::future<> written2(afio::async_write(written, buffers2, 0));
afio::future<> stored(afio::async_sync(written2));
std::array<char, 12> buffer;
afio::future<> read(afio::async_read(stored, buffer, 0));
afio::future<> deletedfile(afio::async_rmfile(afio::async_close(read)));
afio::when_all_p(openfile, resizedfile, written, written2, stored, read)
.get();
std::string contents(buffer.begin(), buffer.end());
std::cout << "Contents of file is '" << contents << "'" << std::endl;
deletedfile.get();
|
waits for file open, resize, write, sync and read to complete,
throwing any exceptions encountered
|
|