7

をファイルに書き込みたいのですstd::wstringが、そのコンテンツを として読み取る必要がありますstd:wstring。これは、文字列がL"<Any English letter>". しかし、ベンガル語、カンナダ語、日本語など、英語以外の文字がある場合に問題が発生します。次のようなさまざまなオプションを試しました:

  1. ファイルへの変換とファイルstd::wstringstd::stringの書き込み、および読み取り時間の読み取りstd::stringと変換std::wstring
    • 書き込みは行われていますが (編集から確認できました)、読み取り時間が間違った文字になっています
  2. wofstreamstd::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;
}
4

5 に答える 5

3

a の出力方法wchar_tはロケールによって異なります。デフォルトのロケール ( "C") は通常、ASCII (Unicode コード ポイント 0x20...0x7E といくつかの制御文字) 以外は受け入れません。

プログラムがテキストを処理するときはいつでも、最初のステートメントは次のようにする main必要があります。

std::locale::global( std::locale( "" ) );

プログラムが標準ストリーム オブジェクトのいずれかを使用する場合、コードは、入力または出力の前に、それらにグローバル ロケールを吹き込む必要もあります。

于 2013-08-02T08:33:18.037 に答える
0

Unicode ファイルを読み書きするには (Unicode 文字を書きたいと仮定して)、fopen_s を試すことができます。

FILE *file;

if((fopen_s(&file, file_path, "w,ccs=UNICODE" )) == NULL)
{
    fputws(your_wstring().c_str(), file);
}
于 2013-08-02T08:29:56.453 に答える
0

後で編集:これはWindows用です(回答時にタグが存在しなかったため)

これらの文字をサポートするロケールにストリームを設定する必要があります。次のようなことを試してください (UTF8/UTF16 の場合):

std::wofstream myFile("out.txt"); // writing to this file 
myFile.imbue(std::locale(myFile.getloc(), new std::codecvt_utf8_utf16<wchar_t>));

そして、そのファイルから読み取るときは、同じことをしなければなりません:

std::wifstream myFile2("out.txt"); // reading from this file
myFile2.imbue(std::locale(myFile2.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
于 2013-08-02T08:34:40.840 に答える
0

wstring または wchar_t を使用しないでください。Windows 以外のプラットフォームでは、最近では wchar_t はほとんど価値がありません。

代わりに、UTF-8 を使用する必要があります。

bool
write_file(
    const char*         path,
    const std::string   data
) {
    try {
        std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary);
        file.exceptions(true);
        file << data;
        return true;
    } catch (...) {
        std::cout << "exception!\n";
        return false;
    }
}


// Read Method

std::string
read_file(
    const char*  filename
) {
    std::ifstream fhandle(filename, std::ios::in | std::ios::binary);

    if (fhandle) {
        std::string contents;
        fhandle.seekg(0, std::ios::end);
        contents.resize(fhandle.tellg());
        fhandle.seekg(0, std::ios::beg);
        fhandle.read(&contents[0], contents.size());
        return contents;
    } else {
        return "";
    }
}

int main()
{
  const char* file_path_1 = "./file_content_1.txt";
  const char* file_path_2 = "./file_content_2.txt";

  std::string data = "হ্যালো ওয়ার্ল্ড"; // linux and os x compilers use UTF-8 as the default execution encoding.

  write_file(file_path_1, data);
  std::string out = read_file(file_path_1);

  std::wcout << "File Content: " << out << '\n';
  write_file(file_path_2, out);
}
于 2013-08-02T17:36:41.887 に答える