How to write in a file

Login to reply to this topic.
Sun, 2008-03-30 10:07
Joined: 2008-01-23
Forum posts: 21

hello,
i have created an HBuf* xyz...

n i have to write its content to a file, every time i need to append the content in my file... can
any one tell me hw can i do it ..
can any one give me small sample code...


Thanks n Regards,
Sapna


Sun, 2008-03-30 18:29
Joined: 2005-12-07
Forum posts: 56
Re: How to write in a file

http://www.newlc.com/topic-567

above might give you some clue. Please search in this forum. You will get lot of code snippets.

Mon, 2008-03-31 04:20
Joined: 2007-09-15
Forum posts: 61
Re: How to write in a file

Check for RFs and RFile APIs.

Tue, 2008-04-01 08:08
Joined: 2007-07-24
Forum posts: 5
Re: How to write in a file

Your code willl be something like this:

void SaveToFile (TDesC &aFileName, TDes8 &aBuf)
{
    RFs fs;
    RFile file;

    fs.Connect ();

    if (file.Create (fs, aFileName, EFileWrite | EFileShareAny) != KErrNone)
    {
        fs.Close ();
        return;
    }

    file.Write (aBuf);

    file.Close ();
    fs.Close ();
}

Tue, 2008-04-01 08:13
Joined: 2008-01-23
Forum posts: 21
Re: How to write in a file

thank u..

if i want to append... i want to use RFileReadStream/RFileReadStream as i want to write TDes16 data to a file...

can u give me small sample code as above....


Thanks n Regards,
Sapna

Tue, 2008-04-01 08:37
Joined: 2007-07-24
Forum posts: 5
Re: How to write in a file

To write to file TDes16 using RFileWriteStream you can use following code

void WriteToFile (TDesC &aFileName, TDes &aBuf)
{
    RFs fs;
    RFileWriteStream file;

    fs.Connect ();
    CleanupClosePushL (fs);

    if (file.Open (fs, aFileName, EFileWrite) != KErrNone)
    {
        CleanupStack::PopAndDestroy ();
        return;
    }
    CleanupClosePushL (file);

    file.WriteL (aBuf);

    CleanupStack::PopAndDestroy (2);
}

But be aware as this code do not appen but write to the file at offset 0 each time it is called.
You can use RFile::Open, RFile::Seek to specify the offset you want to write to and then use RFileWriteStream::Attach to attach write stream to your file...

  • Login to reply to this topic.