Re: Problem reading vector<CString> from file.
std::vector and CString do not store the string within the object. The
object only contains pointers to heap allocations, and the actual strings
are on the heap. The same is true for you vector<double>. Writing your
object to disk only saves the pointers, which is useless.
You need a much more complex file format that will let you write and read
each item within the vectors, plus its type and length. You can do this
yourself with metadata within the file, or you could look into using a
serialization library. Another option is to convert everything to text and
write it as a text or XML file.
"nexolite" <nexolite@discussions.microsoft.com> wrote in message
news:9A942B6D-FB19-43C1-9FB6-355BDBE518C2@microsoft.com...
I have created a class which contains a vector<CString> as a data member
class BillSlip
{
private:
int ItemNameIndex;
vector<double> Weight,Fine,Tch,Qty,Rate,Lab;
vector<CString> ItemName;
CString CustomerName;
char BillNumber[100],Date[20];
double Material_carry,Money_carry;
double Material_paid,Money_paid;
double Material_carry_left,Money_carry_left;
double Material_debit,Money_debit;
public:
/*member functions are also there*/
};
I write the class to a file using
BillSlip BSlip;
fstream BilF;
BilF.open("c:\\BSl.txt",ios::out|ios::binary|ios::app);
BilF.write((char*)&BSlip,sizeof(BillSlip));
BilF.close();
and read the file in the following way:
BillSlipFile.open("c:\\BSlipRec.txt",ios::in|ios::binary|ios::app);
BillSlipFile.read((char*)&BSlip,sizeof(BillSlip));
MessageBox(BSlip.GetCustomerName());
while(!BillSlipFile.eof())
{
BillSlipFile.read((char*)&BSlip,sizeof(BillSlip));
MessageBox(BSlip.GetCustomerName());
}
BillSlipFile.close();
--
Scott McPhillips [VC++ MVP]