fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <codecvt>
  4. #include <algorithm>
  5.  
  6. //VC only.
  7.  
  8. bool ReadStringFromFile(const std::string& FName, std::wstring& R) {
  9. std::wifstream fs;
  10. std::codecvt_utf8_utf16<wchar_t, 0x10ffff, std::consume_header> Cnv;
  11. //auto OL = fs.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8_utf16<wchar_t, 0x10ffff, std::consume_header>()));
  12. auto OL = fs.imbue(std::locale("japanese"));
  13. //static_assert(sizeof(wchar_t) == 2, "this is only for windows.");//Linuxではつかうcvt違うから直してくれ
  14.  
  15. fs.open(FName);
  16.  
  17. if (!fs) {
  18. std::cout << "can't open file." << std::endl;
  19. return false;
  20. }
  21. auto b = std::istreambuf_iterator<std::wistream::char_type>(fs);
  22. auto e = std::istreambuf_iterator<std::wistream::char_type>();
  23. R = std::wstring(b, e);
  24.  
  25. fs.imbue(OL);
  26. return true;;
  27. }
  28. int main() {
  29. std::wstring S;
  30. auto B = std::wcout.imbue(std::locale("japanese"));
  31. if (!ReadStringFromFile((__FILE__), S)) {
  32. return -1;
  33. }
  34. std::wcout << S << std::endl;
  35. std::wcout <<L"------------" << std::endl;
  36. std::reverse(S.begin(), S.end());
  37.  
  38. std::wcout << S << std::endl;
  39.  
  40. std::wcout.imbue(B);
  41.  
  42. return 0;
  43. }
Runtime error #stdin #stdout #stderr 0s 17712KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid