簡體   English   中英

oat++ String::loadFromFile 如何處理 `\0`

[英]how oat++ String::loadFromFile deal with `\0`

當我使用oatpp::String::loadFromFile()時,我想到了一個問題: loadFromFile如何處理\0

我查找源代碼並找到二進制 mod 中的loadFromFile讀取文件,然后將文件數據轉換為char並構建一個oatpp::String object 返回。

如果文件中偶爾有 8 位0並且loadFromFile將其轉換為char並從中構建oatpp::String ,則文件的內容可能會被剪切,因為它在字符串主體中包含\0

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

我想知道是否會出現上述情況,如果會,有沒有什么好的方法可以直接用stream字節填充響應體?

oatpp::String不是以 null 結尾的字符串。 oatpp 會將文件的全部內容加載到字符串中,並將返回完整的內容作為響應。

loadFromFile()實現:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM