Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor joseph.m's Avatar
    Join Date
    Jun 2008
    Posts
    346
    Hi everyone,



    I just want to know how can i avoid errors because of length overflow.


    below is my code.


    Code:
    _LIT( KFileName, "c:\\Data\\Myfile.txt" );
    TBuf<500> aText;
    TBuf<200> filename;
    filename.Copy(KFileName);
    RFs fsSession;
    User::LeaveIfError(fsSession.Connect());
    RFile file;
    if(BaflUtils::FileExists(fsSession, KFileName))
    	{//open the existing
    	if ( file.Open(fsSession, KFileName, EFileRead ) != KErrNone )
    	          return;
    	}
    	else
    	{//create new one
    	file.Create(fsSession, KFileName, EFileWrite );
    	}
    CleanupClosePushL( file );                
    RFileReadStream inputFileStream( file );
    CleanupClosePushL( inputFileStream );
    inputFileStream >> aText;
    inputFileStream.Close();
    file.Close();
    CleanupStack::PopAndDestroy(2);
    fsSession.Close();

    the content of the file may exceed 500 chars how can i make the length dynamically conforms with the length of the chars written in the file?



    Thanks,

    Joseph

  2. #2
    Nokia Developer Expert symbianyucca's Avatar
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    28,673
    the RFile does have Size() function that tells its size, then just use HBufC8 type buffer to read it into.

  3. #3
    Regular Contributor joseph.m's Avatar
    Join Date
    Jun 2008
    Posts
    346
    How can i?


    If im right, declaring TBuf<N>, N should always a constant right?

    can you give me an example code? pls

  4. #4
    Nokia Developer Expert symbianyucca's Avatar
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    28,673
    HBufC8 is heap buffer, so check the SDK docs for it, it is constructed on run time, and you can give any lenght to it (up to avaialbe RAM amount of course)

  5. #5
    Regular Contributor joseph.m's Avatar
    Join Date
    Jun 2008
    Posts
    346
    Yup I know HBuf but in the line


    inputFileStream >> aText;


    aText should be declared as TBuf right?


    HBuf variable can copy data

    HBufC8* aUsr = HBufC8::NewL(aUser.Length());
    CleanupStack::PushL(aUsr);
    aUsr->Des().Copy(aUser);

    is HBuf capable of copying by this syntax?


    inputFileStream >> aUsr;


    is this right?

  6. #6
    Registered User victor.xie's Avatar
    Join Date
    Apr 2008
    Posts
    16
    Refer this example.
    RFile file; // assume it is already open.
    TInt size( 0 );
    User:::LeaveIfError( file.Size( size ) );
    if ( size != 0 )
    {
    HBufC8* buf = HBufC8::NewLC( size );
    TPtr8 bufPtr( buf->Des() );
    file.Read( 0, bufPtr ); // Or
    inputFileStream >> bufPtr;
    CleanupStack::PopAndDestroy();
    }
    Hope it can help you.

  7. #7
    Regular Contributor joseph.m's Avatar
    Join Date
    Jun 2008
    Posts
    346
    I can't exit the app without an error. It always returns an alloc error.


    Here is my code

    Code:
    HBufC8* FilBuff(NULL);
    		
    TEntry MyEntry;
    		User::LeaveIfError(fsSession.Entry(KHelloFileOut,MyEntry));
    		
    if(MyEntry.iSize > 0)
    	{
    	FilBuff = HBufC8::NewL(MyEntry.iSize);
    	TPtr8 ReadPoint(FilBuff->Des());
    fsSession.ReadFileSection(KHelloFileOut,0,ReadPoint,MyEntry.iSize);
    	}

  8. #8
    Nokia Developer Expert symbianyucca's Avatar
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    28,673
    you also need to remember to delete any heap-objects after you have done using them.

    The Alloc error is just informing you that your app has memory leak in it.

  9. #9
    Registered User ykasidit's Avatar
    Join Date
    Jan 2004
    Location
    The surburbs of Bangkok, Thailand
    Posts
    135
    Exactly,

    If between the NewL and the delete doesn't have any leaving function, this would do:

    Code:
    FilBuff = HBufC8::NewL(MyEntry.iSize);
    TPtr8 ReadPoint(FilBuff->Des());
    fsSession.ReadFileSection(KHelloFileOut,0,ReadPoint,MyEntry.iSize);
    delete FilBuff;
    else, it's best to keep the habit of using the CleanupStack:

    Code:
    FilBuff = HBufC8::NewLC(MyEntry.iSize);
    TPtr8 ReadPoint(FilBuff->Des());
    fsSession.ReadFileSection(KHelloFileOut,0,ReadPoint,MyEntry.iSize);
    CleanupStack::PopAndDestroy();//destroy the FillBuff that was pushed by NewLC
    Liberate yourself from the enslavement of World Monopoly!

  10. #10
    Regular Contributor AshramArzephucque's Avatar
    Join Date
    Oct 2007
    Posts
    62
    You are aware that if you open a stream over your file and stream it into the HBufC then it will not work unless the contents of the file were created also by streaming into the file?

    i.e. if it is a .txt file created by some other code/program/pc etc. then you cannot use streaming to read the contents. You will have to do a regular read from RFile.
    Seems you're not streaming now, but originally were, so bear that in mind in case you switch back again.

  11. #11
    Regular Contributor AshramArzephucque's Avatar
    Join Date
    Oct 2007
    Posts
    62
    I disagree - it is not best to get into the habit of using the cleanup stack 'just in case'.
    If everybody did that in all the code on the OS you would probably have thousands of unnecessary pushing/poppings adding processing overhead.

    Quote Originally Posted by ykasidit View Post
    Exactly,

    If between the NewL and the delete doesn't have any leaving function, this would do:

    Code:
    FilBuff = HBufC8::NewL(MyEntry.iSize);
    TPtr8 ReadPoint(FilBuff->Des());
    fsSession.ReadFileSection(KHelloFileOut,0,ReadPoint,MyEntry.iSize);
    delete FilBuff;
    else, it's best to keep the habit of using the CleanupStack:

    Code:
    FilBuff = HBufC8::NewLC(MyEntry.iSize);
    TPtr8 ReadPoint(FilBuff->Des());
    fsSession.ReadFileSection(KHelloFileOut,0,ReadPoint,MyEntry.iSize);
    CleanupStack::PopAndDestroy();//destroy the FillBuff that was pushed by NewLC

  12. #12
    Registered User ykasidit's Avatar
    Join Date
    Jan 2004
    Location
    The surburbs of Bangkok, Thailand
    Posts
    135
    Dear AshramArzephucque,

    Thanks for pointing this out. I agree with you.

    I apologize for my wrong statement, I see now that it's not "best" at all.

    Best Regards.
    Liberate yourself from the enslavement of World Monopoly!

Similar Threads

  1. Error : Cannot find file : for DUMMIES
    By PACALA_BA in forum Symbian C++
    Replies: 7
    Last Post: 2008-07-06, 13:18
  2. Problem with pyobfuscate
    By JOM in forum Python
    Replies: 3
    Last Post: 2008-06-20, 22:47
  3. Problem opening file from a dynamic path
    By didienGanteng in forum Symbian C++
    Replies: 12
    Last Post: 2007-06-27, 11:13
  4. Contacts fields order??
    By timatima in forum Symbian C++
    Replies: 1
    Last Post: 2007-06-08, 13:51
  5. How to create mbm file?
    By qt_raip in forum Symbian Tools & SDKs
    Replies: 13
    Last Post: 2004-05-12, 11:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved