Hi, (This should probably go to comp.lang.c++, but I could not find server
for posting. Sorry.)
I try to read and unpack records from a binary file. The records contain
also a part of
variable, but known length -- say filename -- the length of which is stored
in the fixed
part of the record. (I.e. the record itself has variable length.) The
variable part
is not ASCIIZ -- no terminating zero character.
I am using ifstream object to extract the fixed part of the record into
char buf[known_size]. Then I extract the parts and using the
reinterpret_cast
I store them in member variables of the object that represents the record --
like...
char buf[46];
fin.read(buf, sizeof(buf));
...
m_fname_len = *reinterpret_cast<unsigned short *>(&buf[28]);
No problem here. But once I know the filename length, I want to read that
filename into std::string member variable. I could do something like that
(not heavily tested):
char * p = static_cast<char *>(malloc(m_fname_len));
if (p) {
fin.read(p, m_fname_len);
m_filename.assign(p, m_fname_len);
free(p);
}
Can something similar -- I mean reading a fixed size string from a stream
into std::string -- be done more elegantly, without need to allocate an
extra
buffer?
Thanks for your time and experience,
Petr
--
Petr Prikryl (prikrylp at skil dot cz)