Facing A Descriptor Issue.. plz resolve..

Login to reply to this topic.
Tue, 2007-09-25 13:36
Joined: 2007-04-09
Forum posts: 28

I am facing an error of unwanted garbage being printed along with required in the file.
Details are given with the data entered of size = 18, Length =9.

Class CMyView
{
...
...
...
...
CMyContainer * iContainer;

};

void CMyView::HandleCommandL(TInt aCommand)
{
//Specific To A Case:
RFile   lFileWrite;                       
lFileWrite.Replace(lfsession,KNewFile,EFileWrite|EFileStream);
TInt len = iContainer->Data().Size(); 
TPtr16 lPtr((TUint16*)iContainer->Data().Ptr(),len);
TBuf8<200> buf8;
TBuf16<200> buf16(lPtr.Ptr());       
len = buf16.Length();   //Comes out to be 101
len = CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,buf16);
lFileWrite.Write(buf8);
lFileWrite.Close();

}
Code For Data()....
TDes& CMyContainer :: Data()
{       
//iEdwin <CEikEdwin> All required is done for it.
iEdwin->GetText(iTemp->Des());
//iTemp <HBufC*> memory allocation is done ... iTemp = HBufC::NewL(100)
return iTemp->Des();
}

When it is writting in the file it is writting those 18 bytes along with some garbage values also
Please let me know the solution and the reason why it is happening???


Tue, 2007-09-25 14:26
Joined: 2004-11-29
Forum posts: 1232
Re: Facing A Descriptor Issue.. plz resolve..

The problem is your calls to Ptr() that will return a raw pointer into the buffer.

When filling a descriptor from a raw pointer, it will read until it finds a NULL character.

BUT, descriptors are not NULL-terminated, so it will read on into memory until it happens to find a NULL character, and these extra reads are your garbage.

Try change your code to access the data only through the descriptors instead, (avoid the calls to Ptr()) and your problem will be solved.

Wed, 2007-09-26 04:13
Joined: 2007-04-09
Forum posts: 28
Re: Facing A Descriptor Issue.. plz resolve..

Thanks a lot.
But on changing the code... with rest same as given below.

void CMyView::HandleCommandL(TInt aCommand)
{
//Specific To A Case:
TInt len = iContainer->Data().Size(); 
TPtr16 lPtr((TUint16*)iContainer->Data().Ptr(),len);
TBuf8<200> buf8;
TBuf16<200> buf16(lPtr);       
len = buf16.Length();   //Comes out to be 0

len = CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,buf16);

}

Would u please tell me why the length is coming as 0??
How to resolve it!!!!

Wed, 2007-09-26 08:42
Joined: 2004-11-29
Forum posts: 1232
Re: Facing A Descriptor Issue.. plz resolve..

You are still using calls to Ptr() in that code... Don't do that.

Your code should be able to look like this:

RFile   lFileWrite;                       
lFileWrite.Replace(lfsession,KNewFile,EFileWrite|EFileStream);

TBuf8<200> buf8;
len = CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,iContainer->Data());

lFileWrite.Write(buf8);
lFileWrite.Close();

Also, in your TDes& CMyContainer :: Data(), you should not return iTemp->Des().
Instead do a

return *iTemp;

Consider returning a TDesC& instead of a TDes& too.

Thu, 2007-09-27 11:47
Joined: 2007-04-09
Forum posts: 28
Re: Facing A Descriptor Issue.. plz resolve..

Thanks a lot.
It has solved the problem.
Smiling

  • Login to reply to this topic.