I want to write a struct data in a binary file using wfstram class.
Why The output file is empty?
The following is my code.
#include "stdafx.h"
#include <string>
#include <fstream>
using namespace std;
struct bin_data
{
wstring ch;
size_t id;
};
int main()
{
wfstream f(L"test_bin_file.txt", ios::out | ios::binary);
bin_data *d = new bin_data;
d->id = 100;
d->ch = L"data100";
f.write((wchar_t*)&d, sizeof(struct bin_data));
f.close();
return 0;
}
&d, this is just crapf.write((char*)&d->id, sizeof(size_t));Then write string length, again fixed record and last content of the string usingd->ch.data()std::wfstreamis to translate between Unicode code points, represented by wchar_t on the program side, and UTF-8, GB18030, or other kinds of narrow multibyte encoding, represented by chars, on the filesystem side. Giving it an ios::binary does not change that