Код: Выделить всё
std::mbrtowc
Код: Выделить всё
std::mbsrtowcs
Текстовый файл, который я читаю, состоит из 3369045 байтов и, как ожидается, будет иметь 3324222 (широкие) символы.
Преобразование символа, кажется, работает нормально, но преобразование всех в одном ходе приводит к следующей ошибке:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Aborted (core dumped)
Я понимаю, что он брошен при создании большого std :: wstring . Можно ли это исправить? Если да, как? Кроме того, если я делаю что -то не так, пожалуйста, дайте мне знать. < /P>
код: < /p>
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
static std::istream::pos_type get_file_size(std::istream& in)
{
in.seekg(0, std::ios::end);
auto pos = in.tellg();
in.seekg(0, std::ios::beg);
return pos;
}
std::wstring get_file_contents(const char* buffer, std::size_t size)
{
std::mbstate_t state{};
wchar_t current_wide_char;
std::size_t remaining = size;
std::size_t offset = 0;
std::wstring output{};
while (remaining > 0)
{
std::size_t len = std::mbrtowc(¤t_wide_char, buffer + offset, remaining, &state);
if (len == 0)
{
offset++;
remaining--;
}
else if (len >= 1 && len
Подробнее здесь: [url]https://stackoverflow.com/questions/79502834/length-error-when-trying-to-create-a-stdwstring-to-store-result-of-stdmbsrto[/url]