Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem reading vector<CString> from file.

122 views
Skip to first unread message

nexolite

unread,
Mar 25, 2009, 9:47:01 AM3/25/09
to
I have created a class containing vector<CString> 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 file in the following way:
fstream BillSlipFile;
BillSlip BSlip;

BillSlipFile.open("c:\\BSlipRec.txt",ios::out|ios::binary|ios::app);
BillSlipFile.write((char*)&BSlip,sizeof(BillSlip));
BillSlipFile.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();

during the read operation I getting

"Unhandled exception at 0x004e774f in billingSystem.exe: 0xC0000005: Access
violation reading location 0xcdcdcdcd."

and the debugger is pointing to

IAtlStringMgr* pNewStringMgr = pData->pStringMgr->Clone();
in static CStringData* __cdecl CloneData( __in CStringData* pData ) in
atlsimpstr.h

call stack:
> billingSystem.exe!ATL::CSimpleStringT<char,0>::CloneData(ATL::CStringData * pData=0x00c98918) Line 890 + 0xa bytes C++

billingSystem.exe!ATL::CSimpleStringT<char,0>::CSimpleStringT<char,0>(const
ATL::CSimpleStringT<char,0> &
strSrc="ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ") Line 234 + 0x9 bytes C++

billingSystem.exe!ATL::CStringT<char,StrTraitMFC<char,ATL::ChTraitsCRT<char>
> >::CStringT<char,StrTraitMFC<char,ATL::ChTraitsCRT<char> > >(const
ATL::CStringT<char,StrTraitMFC<char,ATL::ChTraitsCRT<char> > > &
strSrc="ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ") Line 1038 C++
billingSystem.exe!BillSlip::GetCustomerName() Line 52 + 0x11 bytes C++
billingSystem.exe!BillDialog::ShowRecord() Line 25 + 0x19 bytes C++
billingSystem.exe!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x0012f3cc,
unsigned int nID=0x000003f4, int nCode=0x00000000, void (void)*
pfn=0x004c39ba, void * pExtra=0x00000000, unsigned int nSig=0x00000038,
AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000) Line 82 C++
billingSystem.exe!CCmdTarget::OnCmdMsg(unsigned int nID=0x000003f4, int
nCode=0x00000000, void * pExtra=0x00000000, AFX_CMDHANDLERINFO *
pHandlerInfo=0x00000000) Line 381 + 0x27 bytes C++
billingSystem.exe!CDialog::OnCmdMsg(unsigned int nID=0x000003f4, int
nCode=0x00000000, void * pExtra=0x00000000, AFX_CMDHANDLERINFO *
pHandlerInfo=0x00000000) Line 85 + 0x18 bytes C++
billingSystem.exe!CWnd::OnCommand(unsigned int wParam=0x000003f4, long
lParam=0x00020654) Line 2300 C++
billingSystem.exe!CWnd::OnWndMsg(unsigned int message=0x00000111, unsigned
int wParam=0x000003f4, long lParam=0x00020654, long * pResult=0x0012ede4)
Line 1755 + 0x1e bytes C++
billingSystem.exe!CWnd::WindowProc(unsigned int message=0x00000111,
unsigned int wParam=0x000003f4, long lParam=0x00020654) Line 1741 + 0x20
bytes C++
billingSystem.exe!AfxCallWndProc(CWnd * pWnd=0x0012f3cc, HWND__ *
hWnd=0x000e02e6, unsigned int nMsg=0x00000111, unsigned int
wParam=0x000003f4, long lParam=0x00020654) Line 240 + 0x1c bytes C++
billingSystem.exe!AfxWndProc(HWND__ * hWnd=0x000e02e6, unsigned int
nMsg=0x00000111, unsigned int wParam=0x000003f4, long lParam=0x00020654)
Line 389 C++
user32.dll!77d48709()
[Frames below may be incorrect and/or missing, no symbols loaded for
user32.dll]
user32.dll!77d487eb()
user32.dll!77d487ff()
user32.dll!77d70494()
user32.dll!77d4b743()
user32.dll!77d4b7ab()
comctl32.dll!773f6ff6()
comctl32.dll!773f70d8()
comctl32.dll!773f93dd()
billingSystem.exe!AfxGetMainWnd() Line 34 + 0x13 bytes C++
billingSystem.exe!CWinThread::ProcessMessageFilter(int code=0x00000000,
tagMSG * lpMsg=0x0012f0f0) Line 850 + 0x5 bytes C++
billingSystem.exe!_AfxMsgFilterHook(int code=0x77d48709, unsigned int
wParam=0x00020654, long lParam=0x00000202) Line 795 C++
0012f06c()
user32.dll!77d48709()
user32.dll!77d487eb()
user32.dll!77d4ecd2()
user32.dll!77d70494()
user32.dll!77d489a5()
user32.dll!77d553a0()
user32.dll!77d70494()
user32.dll!77d489e8()
user32.dll!77d6e819()
user32.dll!77d65ce2()
billingSystem.exe!CWnd::IsDialogMessageA(tagMSG * lpMsg=0x001558d0) Line
198 C++
billingSystem.exe!CWnd::PreTranslateInput(tagMSG * lpMsg=0x001558d0) Line
4268 C++
billingSystem.exe!CDialog::PreTranslateMessage(tagMSG * pMsg=0x001558d0)
Line 80 C++
billingSystem.exe!CWnd::WalkPreTranslateTree(HWND__ * hWndStop=0x000e02e6,
tagMSG * pMsg=0x001558d0) Line 2882 + 0x14 bytes C++
billingSystem.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x001558d0)
Line 233 + 0x12 bytes C++
billingSystem.exe!CWinThread::PreTranslateMessage(tagMSG *
pMsg=0x001558d0) Line 773 + 0x9 bytes C++
billingSystem.exe!AfxPreTranslateMessage(tagMSG * pMsg=0x001558d0) Line
252 + 0x11 bytes C++
billingSystem.exe!AfxInternalPumpMessage() Line 178 + 0x18 bytes C++
billingSystem.exe!CWinThread::PumpMessage() Line 896 C++
billingSystem.exe!AfxPumpMessage() Line 190 + 0xd bytes C++
billingSystem.exe!CWnd::RunModalLoop(unsigned long dwFlags=0x00000004)
Line 4322 + 0x5 bytes C++
billingSystem.exe!CDialog::DoModal() Line 587 + 0xc bytes C++
billingSystem.exe!BillingApp::InitInstance() Line 423 C++
billingSystem.exe!AfxWinMain(HINSTANCE__ * hInstance=0x00400000,
HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00151f4d, int
nCmdShow=0x00000001) Line 37 + 0xd bytes C++
billingSystem.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__
* hPrevInstance=0x00000000, char * lpCmdLine=0x00151f4d, int
nCmdShow=0x00000001) Line 33 C++
billingSystem.exe!__tmainCRTStartup() Line 324 + 0x35 bytes C
billingSystem.exe!WinMainCRTStartup() Line 196 C
kernel32.dll!7c816d4f()
kernel32.dll!7c8399f3()

Please Help me.

Scott McPhillips [MVP]

unread,
Mar 25, 2009, 9:52:09 AM3/25/09
to
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" <nexo...@discussions.microsoft.com> wrote in message
news:9A942B6D-FB19-43C1...@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]

David Wilkinson

unread,
Mar 25, 2009, 9:54:24 AM3/25/09
to

nexolite:

You cannot serialize an object containing either std::vector or CString in this
manner, because these classes have members that are pointers to the actual data,
and these pointers will not be valid when you read the object back.

Think about CString in particular. Your class BillSlip has a size that is fixed
at compile time. How can an arbitrary length string be represented in this
space? [Answer: it can't.]

--
David Wilkinson
Visual C++ MVP

Tom Serface

unread,
Mar 25, 2009, 10:19:00 AM3/25/09
to
CStringArray and CStringList support serialization, but if it were me I'd do
what some others have suggested and write your own routine that saves the
data and specify the file a little more. This is why a lot of people are
using XML these days (it's straightforward to form, read, and parse back).
When saving you would create one element for each string and then read it
back creating your vector again at runtime. It makes things a lot easy to
debug when you use a readable file format as well.

Tom

"nexolite" <nexo...@discussions.microsoft.com> wrote in message

news:352AB1BB-E87B-4CE9...@microsoft.com...

Jan M

unread,
Mar 25, 2009, 11:02:07 AM3/25/09
to
Something like this works (with a bit more tweaking of course):

USHORT usSize = strName.GetLength();

if(fwrite(&usSize, 1, sizeof(usSize), pF) != sizeof(usSize))
goto Error;

if(usSize)
{
if(fwrite(strName.GetBuffer(), 1, usSize, pF) != usSize)
goto Error;
}

if(fread(&usSize, 1, sizeof(usSize), pF) != sizeof(usSize))
goto Error;

if(usSize)
{
char * szStr = new char[usSize + 1];

if(fread(szStr, 1, usSize, pF) != usSize)
{
delete[] szStr;
goto Error;
}

szStr[usSize] = 0; // Null terminate

strName = szStr;

delete[] szStr;
}


nexolite

unread,
Mar 26, 2009, 8:14:06 AM3/26/09
to
Thanks a lot.
Can u tell me how I can write/read my class to a XML file?
Is there any MFC class for doing it?
or where should I look?

Giovanni Dicanio

unread,
Mar 26, 2009, 12:36:19 PM3/26/09
to

"nexolite" <nexo...@discussions.microsoft.com> ha scritto nel messaggio
news:0BDB27CC-73F8-4D00...@microsoft.com...

> Can u tell me how I can write/read my class to a XML file?
> Is there any MFC class for doing it?
> or where should I look?

There are several options for C++ XML processing (you can use them from MFC
as well):

CMarkup
http://www.codeproject.com/KB/cpp/markupclass.aspx

xerces (and xalan)
http://xerces.apache.org/

libxml2 (and libxslt)
http://xmlsoft.org/

TinyXML:
http://www.grinninglizard.com/tinyxml/

Giovanni

Tom Serface

unread,
Mar 26, 2009, 12:35:26 PM3/26/09
to
Unfortunately, there is nothing "build in" for doing XML. This might help
you:

http://www.codeproject.com/KB/cpp/markuparchive.aspx

If you are going to use the vector you would just increment through it and
write each string to an element. Then when reading back you would recreate
the vector from the XML file.

Tom

"nexolite" <nexo...@discussions.microsoft.com> wrote in message

news:0BDB27CC-73F8-4D00...@microsoft.com...

Joseph M. Newcomer

unread,
Mar 26, 2009, 3:17:27 PM3/26/09
to
The use of the obsolete type char is always suspect. Note that CString is NOT always a
string of 8-bit characters. CStringA is (assuming you are not stuck in VS6)

joe

On Wed, 25 Mar 2009 06:33:01 -0700, nexolite <nexo...@discussions.microsoft.com> wrote:

>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();

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages