バイナリ標準入出力「rn」と「n」の自重変換防止

796 ワード

std::cin.read()ファイルまたはパイプから文字を読み込むと、「r」を読むと自動的に「」に変換され、同じstd::cout.write()は「」と書くと「r」と書きます.
この問題は、バイナリ・モードでファイルを読み書きする場合には発生しません.
std::filebuf fb;
fb.open ("test2.txt", std::ios::in | std::ios::binary); 
より一般的な方法は、ReadFileとWriteFileを使用することです.
HANDLE std_input_handle = ::GetStdHandle(STD_INPUT_HANDLE);
while (size > 0)
{
    DWORD bytes_read = 0;
    if (::ReadFile(std_input_handle, buff, size, &bytes_read, NULL))
    {
        size -= bytes_read;
        buff += bytes_read;
    }
    else
    {
        ret = false;
        break;
    }
}
HANDLE std_output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
::WriteFile(std_output_handle, buff, size, &write_size, NULL);