HBufC8* buffer = HBufC8::NewL(10); // allocate a buffer with size 10 TPtr8 ptr = buffer->Des(); // get a descriptor for the buffer _LIT8(KText, "Hello"); ptr.Copy(KText); // copy the text "Hello" into the buffer delete buffer; // free the buffer memory
RFile file; User::LeaveIfError(file.Open(fs, _L("c:\\data\\file.txt"), EFileRead)); // open a file for reading HBufC8* buffer = HBufC8::NewLC(1024); TPtr8 ptr = buffer->Des(); // allocate a buffer with size 1024 User::LeaveIfError(file.Read(ptr)); // read data from the file into the buffer TInt size = ptr.Size(); // get the size of the data read CleanupStack::PopAndDestroy(buffer); file.Close(); // free the buffer memory and close the fileIn this example, a file is opened for reading using RFile. A buffer of size 1024 is allocated using HBufC8::NewLC(), and a descriptor for the buffer is obtained. Data from the file is then read into the buffer using the Read() method. The size of the data read is obtained using the Size() method of the descriptor. The memory allocated for the buffer is freed using CleanupStack::PopAndDestroy(), and the file is closed using Close(). The HBufC8 class is part of the Symbian C++ library package.