|
|
|
WriteToFile: writes Hello World!
to a file
Found in: examples\Base\BufsAndStrings\WriteToFiles
The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.
// WriteToFile.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// This example writes the text "hello world!" to a file
// Use it as the basis of all examples which depend on just E32 and F32
#include "CommonFramework.h"
#include <f32file.h>
_LIT(KFileName,"WriteToFile.dat");
_LIT(KGreetingText,"Hello World!\n");
// Do the example
LOCAL_C void doExampleL()
{
// The file server session.
RFs fsSession;
// Connect to the file server session.
User::LeaveIfError(fsSession.Connect());
// create the private directory
// on drive C:
// i.e. C:\private\0FFFFF00\
// Note that the number 0FFFFF00 is the
// process security id taken from the 2nd UID
// specified in the mmp file.
fsSession.CreatePrivatePath(EDriveC);
// Set the session path to
// this private directory on drive C:
fsSession.SetSessionToPrivate(EDriveC);
// Use this object to represent
// the file to be written to.
RFile file;
User::LeaveIfError(file.Replace(fsSession,KFileName,EFileWrite|EFileStreamText));
// Note that Write() requires a TDesC8
// type so we need to construct an explicit
// TDesC8 type to represent the data contained
// in the standard (16-bit) descriptor.
TPtrC8 representation((TUint8*)(&KGreetingText)->Ptr(), (&KGreetingText)->Size());
// Write the text ...
User::LeaveIfError(file.Write(representation));
// ... and commit the text.
User::LeaveIfError(file.Flush());
_LIT(KTxt1,"Data written to file\n");
console->Printf(KTxt1);
// Close file
file.Close();
// Delete file (remove comment if you want
// to do this)
//User::LeaveIfError(fsSession.Delete(KFileName));
// close file server session
fsSession.Close();
}
// WriteToFile.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// using relative paths for source and userinclude directories
// No explicit capabilities required to run this.
// Please note that the 2nd UID listed here has not been
// allocated from the central pool of UI's and is not
// guaranteed to be unique.
// The value is used for demonstration purposes only.
//
TARGET WriteToFile.exe
TARGETTYPE exe
UID 0 0x0FFFFF00
VENDORID 0x70000001
SOURCEPATH .
SOURCE WriteToFile.cpp
USERINCLUDE .
USERINCLUDE ..\..\CommonFramework
SYSTEMINCLUDE \Epoc32\include
LIBRARY euser.lib efsrv.lib
CAPABILITY None
// BLD.INF
// Component description file
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
PRJ_MMPFILES
WriteToFile.mmp
This writes the text "Hello World!" to a file. It is used as the basis of all examples which depend on just the Base and the file server.
RFs: file server session
RFile: a single, open file
User: static user functions
TPtrC8: 8 bit constant pointer descriptor
The example writes files to the executable's process private
directory: C:\private\0FFFFF00\.
The second UID in the .mmp file is defined as
0x0FFFFF00 and this is used as the secure ID on which the name of
the private directory is based.
Found in: examples\Base\BufsAndStrings\DynamicBuffers
The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.
// DynamicBuffers.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
#include "CommonFramework.h"
//
// Common literal text
//
_LIT(KTxtNewLine,"\n");
LOCAL_C void writeBuf(CBufBase* aBuf);
LOCAL_C void standardBufferStuffL(CBufBase* aBuf);
LOCAL_C void showExpandL();
LOCAL_C void waitForKey();
//
// do the example
//
LOCAL_C void doExampleL()
{
//
// do flat buffer demonstration
//
_LIT(KTxtFlatBufDemo,"Flat buffer demonstration\n");
console->Printf(KTxtFlatBufDemo);
CBufFlat* flatBuf=CBufFlat::NewL(4);
CleanupStack::PushL(flatBuf);
flatBuf->SetReserveL(32);
_LIT(KTxtFlatBufCapacity,"flat buffer capacity=%d\n");
console->Printf(KTxtFlatBufCapacity,flatBuf->Capacity());
standardBufferStuffL(flatBuf);
CleanupStack::PopAndDestroy();
waitForKey();
//
// do segmented buffer demonstration
//
_LIT(KTxtSegBufDemo,"Segmented buffer demonstration\n");
console->Printf(KTxtSegBufDemo);
CBufSeg* segBuf=CBufSeg::NewL(4);
CleanupStack::PushL(segBuf);
standardBufferStuffL(segBuf);
CleanupStack::PopAndDestroy();
waitForKey();
//
// show ExpandL() and ResizeL()
//
showExpandL();
}
LOCAL_C void standardBufferStuffL(CBufBase* aBuf)
{
//
// insert text into buffer (6 chars)
//
_LIT(KTxtHello,"Hello!");
aBuf->InsertL(0,(TAny*)(&KTxtHello)->Ptr(),(&KTxtHello)->Size());
writeBuf(aBuf);
//
// append more text into buffer (another 6 chars)
//
//
_LIT(KTxtWorld," world");
aBuf->InsertL(10,(TAny*)(&KTxtWorld)->Ptr(),(&KTxtWorld)->Size());
writeBuf(aBuf);
//
// read the 5 characters starting at character
// position 3 from the buffer into a descriptor.
//
//
TBuf<5> des;
aBuf->Read(6,(TAny*)des.Ptr(),des.Size());
_LIT(KTxtRead,"read: %S\n");
console->Printf(KTxtRead,&des);
//
// [over]write 5 characters at character position 6
//
//
_LIT(KTxtFolks,"folks");
aBuf->Write(12,(TAny*)(&KTxtFolks)->Ptr(),(&KTxtFolks)->Size());
writeBuf(aBuf);
//
// delete characters
//
TInt startpos = 5;
TInt length = 6;
startpos <<= 1;
length <<= 1;
aBuf->Delete(startpos,length);
writeBuf(aBuf);
//
// compress
//
aBuf->Compress();
writeBuf(aBuf);
}
LOCAL_C void writeBuf(CBufBase* aBuf)
{
//
// print, segment by segment
//
_LIT(KTxtBuffer,"buffer:");
console->Printf(KTxtBuffer);
TInt bufpos=0;
TPtrC8 bufptr=aBuf->Ptr(bufpos);
while (bufptr.Length()>0)
{
//
// write out this segment of the buffer.
// Note that the descriptor 'display' is built differently
// for Unicode; it also assumes an even number of bytes;
// this is valid because the granularity of the buffer is 4.
//
TPtrC display;
display.Set((TUint16*)bufptr.Ptr(),(bufptr.Length()>>1));
_LIT(KFormat1," [%d,%d] %S");
console->Printf(KFormat1, bufpos, bufptr.Length(), &display);
//
// update position within the buffer
// and the pointer-descriptor.
//
bufpos+=bufptr.Length(); // update position
bufptr.Set(aBuf->Ptr(bufpos)); // should be the next segment
}
console->Printf(KTxtNewLine);
}
LOCAL_C void showExpandL()
{
_LIT(KTxtShowExpand,"Showing ExpandL()\n");
console->Printf(KTxtShowExpand);
//
// allocate the segmented buffer with
// a granularity of 4
//
CBufBase* buf=CBufSeg::NewL(4);
CleanupStack::PushL(buf);
//
// insert text into buffer (12 UNICODE chars)
//
_LIT(KTxtHelloWorld,"Hello world!");
buf->InsertL(0,(TAny*)(&KTxtHelloWorld)->Ptr(),(&KTxtHelloWorld)->Size());
//
// reserve space for (or 16 16-bit chars (32 bytes))
//
buf->ExpandL(12,32); // expand by 32 - may fail
_LIT(KTxtBufExpanded,"Buffer expanded with uninitialized space: ");
console->Printf(KTxtBufExpanded);
writeBuf(buf);
//
// now insert 16 16-bit characters
// one at time.
//
// This is CLEARLY INEFFICIENT but shows
// how successive calls to Write() can be done
// without risk of failing for lack of memory.
//
_LIT(KTxtAtoP,"abcdefghijklmnop");
TBufC<16> source(KTxtAtoP);
for (TInt i=0; i<16; i++)
{
buf->Write((i+6)<<1,(TAny*)&source[i],2);
}
_LIT(KTxtExpandedFilled,"expanded space filled: ");
console->Printf(KTxtExpandedFilled);
writeBuf(buf);
//
// now adjust size down to 18
//
buf->ResizeL(18);
_LIT(KTxtResized,"resized:");
console->Printf(KTxtResized);
writeBuf(buf);
//
// destroy buffer
//
CleanupStack::PopAndDestroy();
}
LOCAL_C void waitForKey()
{
_LIT(KTxtPressAnyKey,"[press any key]");
console->Printf(KTxtPressAnyKey);
console->Getch();
console->Printf(KTxtNewLine);
}
// BLD.INF
// Component description file
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
PRJ_MMPFILES
WriteToFile.mmp
// WriteToFile.mmp
//
// Copyright (C) Symbian Software Ltd 2000-2005. All rights reserved.
// using relative paths for source and userinclude directories
// No explicit capabilities required to run this.
// Please note that the 2nd UID listed here has not been
// allocated from the central pool of UI's and is not
// guaranteed to be unique.
// The value is used for demonstration purposes only.
//
TARGET WriteToFile.exe
TARGETTYPE exe
UID 0 0x0FFFFF00
VENDORID 0x70000001
SOURCEPATH .
SOURCE WriteToFile.cpp
USERINCLUDE .
USERINCLUDE ..\..\CommonFramework
SYSTEMINCLUDE \Epoc32\include
LIBRARY euser.lib efsrv.lib
CAPABILITY None
The example shows how dynamic arrays are constructed and used. It shows a number of operations on a flat dynamic buffer and a segmented dynamic buffer.
The example requires no specific capabilities in order to run - and does not demonstrate any security issues.