how oat++ String::loadFromFile deal with `\0`

322 Views Asked by At

when I use oatpp::String::loadFromFile(),a question comes to my mind: How loadFromFile deal with \0?

I look up the source code and find loadFromFile read file in binary mod, and then cast the file data into char and build a oatpp::String object return.

if there is occasionally 8bit 0 in the file and loadFromFile cast it into char and build oatpp::String from it, the content of file may be cut since it contain a \0 in string body.

auto s = oatpp::String::loadFromFile(FILEPATH)
createResponse(Status::CODE_200, s)

I would like to know if the above scenario will happen, If so, Is there any good way to fill response body with byte stream directly?

1

There are 1 best solutions below

0
lganzzzo On

oatpp::String is NOT a null-terminated string. oatpp will load the full content of the file to string and will return the full content in response.

From loadFromFile() implementation:

String String::loadFromFile(const char* filename) {
  std::ifstream file (filename, std::ios::in|std::ios::binary|std::ios::ate);
  if (file.is_open()) {
    auto result = data::mapping::type::String(file.tellg()); //<-- create string of size of file size.
    file.seekg(0, std::ios::beg);
    file.read((char*) result->data(), result->size()); //<-- read the whole file into string's buffer
    file.close();
    return result;
  }
  return nullptr;
}