2016-04-27 14 views
1

私はこれをファイルに書き込もうとしていますが、役に立たないことがあります。また、これも読み取る必要があります。別のベクトルを含む構造体のベクトルをバイナリファイルに書き込む

ofstream fileBack; 
fileBack.open("file.txt", ios::in|ios::binary|ios::trunc); 

fileBack.write(reinterpret_cast<char*>(&accounts), accounts.size()*sizeof(accounts)); 
fileBack.close(); 

そして、私は、ファイルを開いたとき、私がに入れた情報を格納するのに十分なほぼ大きくないので、私は、これは間違っている知っている:ここでは私が今持っていることは、構造体

struct details 
{ 
    float balance=0; 
    vector<string> history; 
    string pin; 
}; 
struct customer 
{ 
    int vectorID; 
    string name; 
    char type; 
    details detail; 
}; 
vector<customer> accounts; 

をですそれ。 すべての助けがありがとう、ありがとうございます。

+0

あなたは「間違った道」を見つけました。 – LogicStuff

+0

ええ、私はちょうど何かが棒を望んで暗闇の中でショットを撮っていると思った – Lansrow

答えて

1

非常に簡単な方法はBoost Serializationを使用することです。

std::ofstream ofs("filename", std::ios::binary); // binary file open 
.... 
// save data to archive 
{ 
    boost::archive::text_oarchive oa(ofs); 
    // write class instance to archive 
    oa << yourCustomerClass; 
} 

、反対を:あなたはあなたがちょうどできるファイルに追加したいときに

void details::serialize(Archive & ar, const unsigned int version) { 
    ar & balance; 
    ar & history; 
    ar & pin; 
} 


void customer::serialize(Archive & ar, const unsigned int version) { 
    ar & vectorID; 
    ar & name; 
    ar & type; 
    ar & detail; 
} 

:あなたは、例えばシリアライズを処理するために、あなたの各クラスにメンバ関数を定義する必要がありますファイルを読む。

+0

ありがとう!私はこれを試して実装し、結果を返すつもりです – Lansrow

関連する問題