Учитывая ваши требования, я не думаю, что wfstream - это путь. Рекомендуется использовать что-то вроде следующего фрагмента кода.
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring str(L"hello");
size_t size1 = str.length();
char data[] = { 0x10, 0x20, 0x30 };
size_t size2 = 3;
FILE* output = NULL;
if (_wfopen_s(&output, L"c:\\test.bin", L"wb") == 0) {
fwrite(&size1, sizeof(size_t), 1, output);
fwrite(str.c_str(), size1 * sizeof(wchar_t), 1, output);
fwrite(&size2, sizeof(size_t), 1, output);
fwrite(data, size2, 1, output);
fclose(output);
}
FILE* input = NULL;
if (_wfopen_s(&input, L"c:\\test.bin", L"rb") == 0) {
fread(&size1, sizeof(size_t), 1, input);
wchar_t* wstr = new wchar_t[size1 + 1];
fread(wstr, size1 * sizeof(wchar_t), 1, input);
std::wstring str(wstr, size1);
delete[] wstr;
fread(&size2, sizeof(size_t), 1, input);
char* data1 = new char[size2];
fread(data1, size2, 1, input);
std::wcout << str.c_str() << std::endl;
for (size_t i = 0; i < size2; ++i) {
std::wcout << std::hex << "0x" << int(data1[i]) << std::endl;
}
delete[] data1;
fclose(input);
}
return 0;
}
Это выводит:
hello
0x10
0x20
0x30