をファイルに書き込みたいのですstd::wstring
が、そのコンテンツを として読み取る必要がありますstd:wstring
。これは、文字列がL"<Any English letter>"
. しかし、ベンガル語、カンナダ語、日本語など、英語以外の文字がある場合に問題が発生します。次のようなさまざまなオプションを試しました:
- ファイルへの変換とファイル
std::wstring
へstd::string
の書き込み、および読み取り時間の読み取りstd::string
と変換std::wstring
- 書き込みは行われていますが (編集から確認できました)、読み取り時間が間違った文字になっています
- wofstream
std::wstring
に書き込むと、これは次のような母国語の文字にも役立ちません。std::wstring data = L"হ্যালো ওয়ার্ল্ড";
プラットフォームはmacとLinux、言語はC++
コード:
bool
write_file(
const char* path,
const std::wstring data
) {
bool status = false;
try {
std::wofstream file(path, std::ios::out|std::ios::trunc|std::ios::binary);
if (file.is_open()) {
//std::string data_str = convert_wstring_to_string(data);
file.write(data.c_str(), (std::streamsize)data.size());
file.close();
status = true;
}
} catch (...) {
std::cout<<"exception !"<<std::endl;
}
return status;
}
// Read Method
std::wstring
read_file(
const char* filename
) {
std::wifstream fhandle(filename, std::ios::in | std::ios::binary);
if (fhandle) {
std::wstring contents;
fhandle.seekg(0, std::ios::end);
contents.resize((int)fhandle.tellg());
fhandle.seekg(0, std::ios::beg);
fhandle.read(&contents[0], contents.size());
fhandle.close();
return(contents);
}
else {
return L"";
}
}
// Main
int main()
{
const char* file_path_1 = "./file_content_1.txt";
const char* file_path_2 = "./file_content_2.txt";
//std::wstring data = L"Text message to write onto the file\n"; // This is happening as expected
std::wstring data = L"হ্যালো ওয়ার্ল্ড";
// Not happening as expected.
// Lets write some data
write_file(file_path_1, data);
// Lets read the file
std::wstring out = read_file(file_path_1);
std::wcout<<L"File Content: "<<out<<std::endl;
// Let write that same data onto the different file
write_file(file_path_2, out);
return 0;
}