-
I'm curious how I would perform text-based IO (i.e., read into a string). My initial attempt was something like: llfio::file_handle fh = llfio::file({}, absolute_path, mode, creation, caching, flags).value();
std::string buffer;
buffer.resize(fh.maximum_extent().value());
const auto bytesread = llfio::read(fh, 0, {{reinterpret_cast<llfio::byte*>(buffer.data()), buffer.size()}}, std::chrono::minutes(1)).value();
buffer.resize(bytesread);
fh.close().value();
return buffer; However, I'm considering rewriting this to use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Strings in C++ are currently just bytes, and Or just do reinterpret cast as you currently do. |
Beta Was this translation helpful? Give feedback.
-
What I now do is this: namespace llfio = LLFIO_V2_NAMESPACE;
// ...
llfio::file_handle fh{llfio::file({}, absolute_path, mode, creation, caching, flags).value()};
std::string buffer;
buffer.resize(fh.maximum_extent().value());
auto buffer_view{std::as_writable_bytes(std::span{buffer})};
const auto bytesread{
llfio::read(fh, 0, {{buffer_view.data(), buffer_view.size()}}, std::chrono::minutes(1))
.value()};
if (bytesread == 0) {
return "";
}
buffer.resize(bytesread);
fh.close().value();
return buffer; I'm not sure if this has any performance overhead, or if I can get an even greater amount of performance out of it, but it's quite a nice, low-level API and I can see myself very much using llfio in the future. The trick will be figuring out the right tunables, but this works for now. |
Beta Was this translation helpful? Give feedback.
Strings in C++ are currently just bytes, and
std::span
has some helper member functions to return a span ofT
as a span of bytes. So you could - if you wanted to - turn the string buffer into a span of char, take that into a span of bytes, read into that.Or just do reinterpret cast as you currently do.