Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wifstream problem

Status
Not open for further replies.

ionutdinulescu

Programmer
Joined
Jun 11, 2002
Messages
59
Location
GB
Hello.
I write some information into a binary file:
int iLevel
WideString sCode;
wofstream *f=new wofstream(s,ios::binary);
....
f.write((wchar_t*)&(iLevel),sizeof(int));
iLen=t->sCode.Length();
f.write((wchar_t*)&iLen,sizeof(int));
f.write(sCode.c_bstr(),iLen);

When I want to get it back:
wifstream* f=new wifstream(s.c_str(),ios::in|ios::binary);
wchar_t* c;
...
f->read((wchar_t*)&iLevel,sizeof(int));
f->read((wchar_t*)&iLen,sizeof(int));
f->read(c,iLen);
sCode=WideString(c);
I obtain the right iLevel and iLen, but Access violation occurs when trying to read variable c.
Can you find the problem ?

 
Ion, look at:
wchar_t* c;

you have not initialized variable c. In my opinion you should alloc space for it.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Now I initialized c with:
wchar_t* c=L"";
There is another problem. If I write to the file 0 and 'Dep1', I obtain the following results:

iLevel=0
iLen=4
c=Dep1d\x6200\x7361\x6369\x735f\x7274\x6e69g
If I write 0 and 'De', then I obtain
ilevel=0
iLen=2
c=De\x6464\x.........

I really don't know the cause.
 
You initialized it, but you still didn't allocate any space for it (except maybe a byte).
 
Ion,
wchar_t* c=L"";//there are one single letter allocated
//alloc at least 128 characters under it
c = new wchar_t[100];

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top