Hi,
how can I store a class of string vectors as a bin or text file? I tried a .write/.read method which compiles fine, but I get a run time access violation error. Please see code below. I am using VC++ 6.0 and 7.0. Many thanks!
tord
Code:
vector<string>Feeder(1500); int Min1 = 0; string sMess1;
vector<string>Reader(1500);
void CfeedertestDlg::OnBnClickedButton1()
{
mimix Mupp(Feeder);//declare empty class with empty string vector
Feeder[Min1] = sMess1;//int Min1 and string sMess1 defined by other buttons, set in vector.
Mupp.SetD1(Feeder); //set vector with string sMess1 at position Min1 in class Mupp.
ofstream fout1("MuppMem", ios::binary);//prepare file
fout1.write((char*) &Mupp, sizeof Mupp);//write class to file
fout1.close();
Reader = Mupp.GetD1();//run time error here if loaded from bin/text class store file!
string sMess2 = Reader[Min1];//get string and display in message box:
char A[255];
ofstream fout("bupp.txt");
fout << sMess2;
fout.close();
ifstream fin("bupp.txt");
fin >> A;
CString cMess = A;
MessageBox(A);
Reader.erase(Reader.begin(), Reader.end());
Feeder.erase(Feeder.begin(), Feeder.end());
}
mimix::mimix(vector<string>initD1)
{itsD1 = initD1;
}
mimix::~mimix(void)
{
}
void CfeedertestDlg::OnBnClickedButton6()
{
string sMess3;
mimix Mepp(Feeder);
ifstream fin2("MeppMem",ios::binary);
fin2.read((char*) &Mepp, sizeof Mepp);
fin2.close();
Reader = Mepp.GetD1();//problem here, but only if retrieved from file, see above!
char B[255];
ofstream fout4("buppo.txt");
fout4 << sMess3;
fout4.close();
ifstream fin4("buppo.txt");
fin4 >> B;
CString cMess2 = B;
MessageBox(B);
}
//from class file:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class mimix
{
public:
mimix(vector<string>initD1);
~mimix(void);
void SetD1(vector<string>D1) {itsD1 = D1;}
vector<string> GetD1() const {return itsD1;}
private:
vector<string>itsD1;
};