0

我想知道用 std::string 和 std::wstring 对象写入同一个文件是否肯定是安全的。我有一个这样的片段:

void foo(std::string&& msg) {
    std::ofstream file{ "log.txt", std::ios_base::app };
    file << msg << '\n';
}

void foo(std::wstring&& msg) {
    std::wofstream file{ "log.txt", std::ios_base::app };
    file << msg << '\n';
}

int main() {
    foo(std::string{ "sample string text" });
    foo(std::wstring{ L"sample wstring text" });
}

我不确定使用此代码时编码是否没有问题,或者在特定情况下可能会出现这些问题。有没有人知道与之相关的危险?

4

0 回答 0