Re: [OBORONA-SPAM] Re: [Dev-C++] Re: RE: need advice on data storage
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-03-31 17:52:31
|
If anyone wants to store binary information, there is no need to encode it
in hex or similar. Disk drives are 8-bit clean :)
/Per W
On Fri, 1 Apr 2005, Denis Korzunov wrote:
> Hi. If you want to store some binary information you can store it in
> hex or base64 or uue. Then you'll be able to work with it as with usual
> ascii strings, store it in files, etc.
> Simple example:
>
> class A
> {
> char* binary_stuff;
> int len;
> ...
> public:
> friend ostream& operator<<(ostream& os, A& a);
> friend istream& operator>>(istream& is, A& a);
> ...
> };
>
> ostream& operator<<(ostream& os, A& a)
> {
> for(int i=0;i<len;++i)
> {
> char* c[3]; _itoa(a.binary_stuff[i],c,16);
> os<<"0x"<<c<<" ";
> }
> os<<'\n';
> return os;
> }
> istream& operator>>(istream& is, A& a)
> {
> string temp; is=getline(temp); int i=0;
> while(pos=temp.find(" ")!=-1){
> a.binary_stuff[i++]=(temp.substr(0,pos)[3]-31)*16+
> (temp.substr(0,pos)[4]-31);
> temp=temp.substr(pos+2);
> return is;
> }
>
> int main()
> {
> ifstream f1("if.txt");
> ofstream f2("of.txt");
> A a; f1>>a;
> ...
> f2<<a;
> return 0;
> }
> --
> Best regards,
> Denis
|