Home > Blockchain >  How to store class object having string in binary file?
How to store class object having string in binary file?

Time:12-26

I'm storing my class object in the binary file but I'm getting weird results when I load the data.
Following Code is Loading and Saving Data:

#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
using namespace std;

template <class C>
void Load(const char fileName[], C& obj)
{
    ifstream in;
    in.open(fileName, ios::in | ios::binary);
    in.read(reinterpret_cast<char*>(addressof(obj)), sizeof(obj));
    in.close();
    return;
}

template <class T>
void Save(const char fileName[], T obj)
{
    ofstream out;
    out.open(fileName, ios::out | ios::binary);
    out.write(reinterpret_cast<char const*>(addressof(obj)), sizeof(obj));
    stringstream ss;
    out.close();
    return;
}

class Contact {
public:
    int CompareTo(Contact obj)
    {
        return 1;
    }
    string ss;
    int rollNum;
};

class Data {
public:
    Data()
    {
    }
    Contact arr[10];
};

int main()
{
    const char fileName[] = "ContactMG.dat";
    /*
     Data *T = new Data();
    
     for(int i=0;i<10;i  )
          T->arr[i].ss = "fahad";
       Save(fileName , *T);
    */

    Data* d = new Data();
    Load(fileName, *d);
    for (int i = 0; i < 10; i  )
        cout << d->arr[i].ss << endl;
}

/*
 Console outPut:
ⁿx

 p²x
   σß╥Z∙
  ░▒▓│┤
   >
☺Y╩
░‼╩

*/

/* Binary File
   @®     ®     ®     
*/

I want to ask how I can store this object in the binary file and load it?

I'm pretty sure the problem is with string but I don't know how to fix it! I have already known to store strings in binary files but don't know how to store class objects which have string in it

CodePudding user response:

After struggling with this problem I end up with a simple solution. I made a class 'String ' which almost works likes 'std::string' and it's also working with binary files

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

#define MAXSIZE 24

template<class T>
class IComparable{
public:
    virtual int CompareTo(T& obj) const = 0;
};

class String : IComparable<String> {
 public:

String(void){
    _str = new char[MAXSIZE];
    length = MAXSIZE;
    itr = -1;
}

String(const size_t size){
    _str = new char[size];
    length = MAXSIZE;
    itr = -1;
}

void Push_Back(const char c){
    if(itr >= length-1) return;
    itr  ;
    _str[itr]  = c;
}

void Push_Top(const char c){
    if(itr >= length-1) return;
    itr  ;
    for(int i=0;i<itr;i  )
       _str[i 1] = _str[i];
    _str[0] = c;
}

void operator =(const string ss){
    itr = -1;
    for(auto i:ss)
        Push_Back(i);
}

void operator  = (const string ss){
    for(auto i:ss)
        Push_Back(i);
}

int CompareTo(String& obj) const override{
    // compare
    return 0;
}

  template<class Func>
void For_Each(Func CallBack){
    if(IsEmpty()) return;
    for(int i=0;i<itr 1;i  )
       CallBack(_str[i]);
} 

friend ostream& operator << (ostream& out , String ss){
    for(int i=0;i<ss.itr 1;i  )
        out<<ss._str[i];
    return out;
}

friend istream& operator >>(istream& in , String ss){
      string str;
      in.sync();
      in>>str;
      ss = str;
      return in;
}

bool IsEmpty(){return itr == -1;}

~String(){
    delete _str;
}

private:

  char *_str;
  int length , itr;

  // Helper
  void Swap(char& val1 , char& val2){
    char temp = val1;
    val1  = val2;
    val2 = temp;
  }

};

int main(){
  String ss;
  ss = "hello world";
  ss = "..";
  cout<<ss<<endl;
  ss = "HELLO WORLD";
  cout<<ss<<endl;
 }

Throughout the project, I'll use this String instead of std::string

  • Related