Last active
July 1, 2023 23:20
-
-
Save jonwis/f0985179d50501e36a0e32bac79a7c79 to your computer and use it in GitHub Desktop.
UTF-8 Encoded std::wifstream support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ifstream> | |
#include <string> | |
// Opens a file by path name. Assumes the file is UTF-8 encoded. If the file | |
// has a BOM, it'll be skipped so the first access to the file is positioned | |
// correctly. | |
std::wifstream open_utf8_with_maybe_bom(std::filesystem::path const& path) | |
{ | |
std::wifstream ss; | |
ss.imbue(std::locale(".utf-8")); | |
ss.open(path); | |
if (ss.good() && (ss.peek() == 0xFEFF)) | |
(void)ss.get(); | |
return ss; | |
} | |
// Use notepad to create a file called "test-app.txt", and save it with the "UTF-8 with BOM" encoding | |
void do_test() | |
{ | |
auto stream = open_utf8_with_maybe_bom(std::filesystem::path("test-app.txt")); | |
std::wstring t; | |
while (std::getline(stream, t)) | |
{ | |
std::wcout << L"Line " << t << L" " << t.length() << std::endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Take the utf8wifstream.cpp, and add: | |
#include <sstream> | |
#include <winrt/Windows.Data.Json.h> | |
void read_json_file() | |
{ | |
auto stream = open_utf8_with_maybe_bom("test-app.json"); | |
std::wstringstream content; | |
content << stream.rdbuf(); | |
auto obj = winrt::Windows::Data::Json::JsonObject::Parse(content.str()); | |
std::wcout << obj.ToString().c_str() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh and because I love cryptic one-liners:
auto obj = winrt::Windows::Data::Json::JsonObject::Parse((std::wstringstream{} << stream.rdbuf()).str());