c++ - vector<char> to int vs. vector<wchar_t> to int -
so had next function:
template < typename t > void someclass::readbinary(t& res) { size_t sbytestoread = sizeof(t); res = 0; std::vector<char> vcbuffer(sbytestoread); m_fstream.read(&vcbuffer[0], sbytestoread); // tbd - utilize reintepert_cast res = reinterpret_cast<t>(&vcbuffer[0]); }
where m_fstream
std::fstream
.
this function worked expected. when changed m_fstream
type std::wfstream
, adjust function little:
template < typename t > void someclass::readbinary(t& res) { size_t sbytestoread = sizeof(t); res = 0; std::vector<wchar_t> vcbuffer(sbytestoread); // type of vector changed m_fstream.read(&vcbuffer[0], sbytestoread); // tbd - utilize reintepert_cast res = reinterpret_cast<t>(&vcbuffer[0]); }
i different result (meaning different int value res
). kinda expected have issue here. reading bytes file different in both cases.
i'm little lost , don't know how resolve this. idea?
notet
either: uint8_t
, uint16_t
& uint32_t
if want read binary value stream, need this:
template < typename t > void someclass::readbinary(t& res) { m_fstream.read(&res, sizeof(res)); }
there no need allocate vectors of , no need reinterpret cast anything.
this std::fstream
only. not utilize std::wfstream
binary input, utilize std::fstream
. sizeof(t)
might odd sizeof(wchar_t)
might even, cannot read exact amount of bytes need wfstream
. if absolutely must utilize std::wfstream
, absolutely sure sizeof(t)
divides sizeof(wchar_t)
can seek like
m_fstream.read(&res, sizeof(res)/sizeof(decltype(m_fstream)::char_type));
but have never tried this.
c++ type-conversion fstream
No comments:
Post a Comment