I use a self-made error/log-class for my project which is also be able to store/load itself into/from a file. To make it easier for me when handling more objects I want to pass a file stream by-reference to this function which then uses the write/read member of the stream.
Save with:
in.open(L"XError.err",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
Load with:
in.open(L"XError.err",std::ios::in | std::ios::out | std::ios::binary);
For saving:
unsigned int HError::SaveToBufferW(std::wfstream& file)
{
_ErrorSaveStruct ess = {0};
ESS.IsUNICODE = true;
ESS.ItemCount = 9999999;
file.write((wchar_t*)&ess,sizeof(_ErrorSaveStruct) / sizeof(wchar_t));
return 0;
}
For loading:
int HError::LoadFromBufferW(std::wfstream& file)
{
_ErrorSaveStruct ess = {0};
file.read((wchar_t*)&ess,sizeof(_ErrorSaveStruct) / sizeof(wchar_t));
return 0;
}
I checked the file and found out that nothing but whitespaces is written.
When I read/write a Unicode-string everything works and the string as well is readable in the file.
Edit: here you go
struct _ErrorSaveStruct
{
unsigned int MsgSize;
unsigned int TimeSize;
unsigned int LastErrorSize;
int ItemCount;
int errState;
bool InitMsg;
bool IsUNICODE;
};
ok, used another answer to bypass my problem in some way:
I really learned something about streams and file IO. Thinking of other solutions, i also could have passed single parameters (int,bool) to wfstream instead of a whole struct. It would have been possible as well to use std::fstream with binary flag set and pass a wchar_t string via write(). Anyway, this solution works well enough for me and I’m really happy about that.