c++builder - How to use AnsiString to store binary data? -
i have simple question.
i want use ansistring container binary data. load such data tmemorystream or tfilestream , save ansistring after processing. works fine, haven't found problem that.
but i've seen using sparcles debates use sysutils::tbytes
instead. why? sysutils::tbytes
has fewer useful methods can use manipulate data stored inside example ansistring
. half-finished container, compared ansistring.
is problem should care conversion regular string or there else why should use less-than-adequate tbytes
instead? not make conversions of ansistring other string types - quoted possible problem elsewhere.
an example of how load data:
ansistring data; boost::scoped_ptr<tfilestream> fs(new tfilestream(filename, fmopenread | fmsharedenywrite)); data.setlength(fs->size); fs->read(data.c_str(), fs->size);
an example how save data:
// fs wants void * have use data.data() instead of data.c_str() here fs->write(data.data(), data.length());
so should safe store binary data correct?
i want use ansistring container binary data.
one word - don't! bite someday. use more appropriate container, such tbytes
, tmemorystream
, std::vector<byte>
, etc.
works fine, haven't found problem that.
consider lucky. c++builder 2009 onwards, ansistring
codepage-aware, , cause data conversions if not careful when passing ansistring
around. sooner or later, slip , risk corrupting binary data.
but i've seen using sparcles debates use sysutils::tbytes instead. why?
because actual raw binary container meant raw bytes.
sysutils::tbytes has fewer useful methods can use manipulate data stored inside example ansistring.
you should not manipulating binary data text begin with. , since using things boost , stl, should consider using binary containers instead. have more functions available.
that being said, xe7 introduce new functions manipulating delphi-style dynamic arrays (like tbytes) including inserts, deletes, , concatenations:
string-like operations supported on dynamic arrays
it not new functions made c++builder's dynamicarray
class (which tbytes
typedef
of), though.
it half-finished container, compared ansistring.
ansistring
container of text characters. period. has been, be. people abuse taking advantage of fact sizeof(char)==sizeof(byte)
. worked point, has become dangerous in recent years continue abusing it.
is problem should care conversion regular string or there else why should use less-than-adequate tbytes instead?
that, , fact embarcadero has been phasing out ansistring
since 2009. 8bit strings disabled in mobile compilers, matter of time before desktop compilers follow suit.
why wanting manipulate raw bytes strings begin with? can provide example of can ansistring
cannot tbytes
?
so should safe store binary data correct?
in specific example, yes (and yes, can use c_str()
instead of data()
when calling fs->write()
).
Comments
Post a Comment