unsigned char* temp = fileUtils->getFileData("levels.json", "r", &size); const char* temp1 = reinterpret_cast<const char*>(temp); wchar_t* levelData = new wchar_t[size]; int last_char_size = 0; mbtowc(NULL, 0, 0); for (wchar_t* position = levelData; size > 0; position++) { last_char_size = mbtowc(position, temp1, size); if (last_char_size <= 0) break; else { temp1 += last_char_size; size -= last_char_size; } } if (last_char_size == -1) { std::cout << "Invalid encoding" << std::endl; } delete[] temp;
The marked line (*) depends on whether fileUtils->getFileData block of memory for temp , and the fileUtils object fileUtils not manage it on its own. - What is most likely. However, you should check the documentation.
size should be a sufficient size for the levelData array, while whithin [] you specify the number of elements in the array, not the number of bytes (aka char s). - In this case, this is the number of wide characters. Which couldn't be bigger then read char s.
Another thing you should know about, fileUtils->getFileData is probably reading a binary date. Thus, the text in temp not followed by 0. Thus, subsequent string functions like wcstok - calling it will take your foot off.
And with each other. If you are not familiar with the construction
function_on_arrays( target, source, size )
Remember that your program in C / C ++ does not know the sizes of target and source . But you probably do not want the function to do anything beyond them. So that's what for size basically. - Your manual way to say how many elements you want to perform so as not to go beyond the data arrays.
Edit: The earlier solution was wrong, as it mistakenly treated the last mbstowcs parameter as the number of characters in the source.
source share