I want to load a html file to memory (in fact a wchar_t string). And this is the code:
size_t myGetFileSize(const wchar_t *wcPath)
{
struct _stat fileinfo;
_wstat(wcPath, &fileinfo);
return (fileinfo.st_size);
}
int LoadUtf8FileToString(const wchar_t *wcFilename, wchar_t **wcBuffer)
{
FILE* file = _wfopen(wcFilename, L"rtS, ccs=UTF-8");
if (file == NULL)
return (0);
size_t filesize = myGetFileSize(wcFilename);
if (filesize > 0)
{
*wcBuffer = (wchar_t*) malloc(filesize * sizeof(wchar_t));
size_t nRead = fread(*wcBuffer, sizeof(wchar_t), filesize, file);
realloc(*wcBuffer, nRead * sizeof(wchar_t));
}
fclose(file);
return(1);
}
And when I navigate it to a iwebbrowser2, it show all page and 4 empty square at the end of page! I googled and find a string class called wstring, and using it like this way:
std::wstring wString;
/////////////////////
wString->resize(filesize);
size_t wchars_read = fread(&(wString->front()), sizeof(wchar_t), filesize, file);
wString->resize(wchars_read);
wString->shrink_to_fit();
and navigate it to iwebbrower2, everythings will be OK! But I don't like to use any class in my program! So, What is wrong with my code, please?