×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Question Reading & Writing a file (uRGENT)

    Hello everybody,

    I have a structure of the following type,

    data{
    TInt A;
    TInt B;
    Tint C;
    TBool D;
    TBool E;
    }


    Now i want this structure to be written to a file and read back. Can anyone help me in solving this problem? lease provide some code so it will be clear.

    Thanks in advance

  2. #2
    Nokia Developer Expert
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,143
    maybe you could have a look into streams, such as RFileReadStream & RFileWriteStream. I'm quite sure there are also example applications utilizing them.

    yucca

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    I m able to write to file, but not getting the exact code to read

    This is the code for writing, where iData contains the above declared structure,

    RFileWriteStream Stream;
    TInt Error = Stream.Replace(CEikonEnv::Static()->FsSession(),SaveSettingFile,EFileWrite);
    if (KErrorNone == Error)
    {
    TRAP(Error,Stream.WriteL(TUint 8*)&(iData),sizeof(class ListData)));
    Stream.close();
    }

    can u give me the code to read,...

    thanks in advance
    thanks for replying yucca

  4. #4
    Nokia Developer Expert
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,143
    see this for a short example: http://forum.newlc.com/viewtopic.php?t=5720

    basically, I think you could just write the values after each other in order and then read them out in similar way.

    yucca

  5. #5
    Regular Contributor
    Join Date
    Feb 2005
    Posts
    328
    An alternative to streams...

    class TData // in Symbian this is a T-class
    {
    TInt A;
    TInt B;
    Tint C;
    TBool D;
    TBool E;
    }

    //

    void CMyClass::SaveToFileL(const TData& aData)
    {
    TPckg<TData> dataBuf(aData);

    RFile file;
    User::LeaveIfError(file.Replace(iFs, KMyFileName, EFileWrite | EFileStream));
    CleanupClosePushL(file);
    User::LeaveIfError(file.Write(dataBuf));
    CleanupStack::PopAndDestroy(&file); // closes file
    }

    you could just reverse the above to read the data back in

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    38

    What about Line by line?

    Hello,

    My requirement is to read a file one line at a time. I have the following code however when I read the last line (which contains EOF) the program halts with epoc error. I cannot TRAP. Any ideas?

    Thanks,
    RE

    TInt fSize;
    TInt iReadBytes = 0;
    RFile file;
    RFs fsSession;
    _LIT( KContactFile,"\\system\\Apps\\PSCLIENT\\contacts.txt");
    User::LeaveIfError(fsSession.Connect());
    TInt err = file.Open(fsSession,KContactFile, EFileRead);
    if (err != KErrNotFound)
    {
    // read the Contact file
    file.Size(fSize);
    RFile f = file;
    RFileReadStream stream(f);
    TBuf8<128> *buf = new(ELeave)TBuf8<128>;
    do {
    TRAPD(err, stream.ReadL(*buf,(TChar)'\n'));
    if (err == KErrEof)
    {
    int i = 1;
    }
    iReadBytes += buf->Length();
    buf->Delete(0, 128);
    } while (iReadBytes < fSize);
    stream.Close();
    fsSession.Close();
    }

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    9

    Reading and writing

    Hi

    I learned how to read and write data while programming Epoc for Psion Revo some years ago. I still use the same way and it works perfect.

    Search for information on Externalize/Internalize. Read and implement once and I guarantee you will like it.


    /T

  8. #8
    Regular Contributor
    Join Date
    Jun 2004
    Posts
    135

    Re: Reading & Writing a file (uRGENT)

    Hello,

    How to write the following struct into a file ? can I use TPckgBuf ? and how.

    struct TLicensePlugin
    {
    TInt iPluginId;
    TInt iSubscriptionType;
    TInt iDaysLeft;
    };

    struct TLicenseLocal
    {
    TInt iVersion;
    TBuf<50> iLastTime;
    TBuf<50> iUser;
    TBuf<50> iPwd;
    CArrayFixFlat<TLicensePlugin>* iPlugins;
    };

    If I don't include the last line, iPlugins, the structure is saved and loaded perfectly using the code posted before my post.

    But I have this array of TLicensePlugin, that is another struct.

    Do I need to write separately into a loop the elements of this array or can I insert somehow into the whole TPckgBuf buffer?

    thanks.

  9. #9
    Regular Contributor
    Join Date
    Feb 2005
    Posts
    328

    Re: Reading & Writing a file (uRGENT)

    Hi

    TLicenseLocal in this instance is really not a T class/struct but rather a C class/struct. This is because is contains (owns) a C class which requires proper destruction.

    I suggest you implement a new class called CLicenseLocal deriving from CBase. Add to this InternalizeL() and ExternalizeL() functions as well as a destructor that properly deletes all members.

    In the InternalizeL and ExternalizeL you'll need to iteratively write each plugin data to the file. You'd probably also want to write the count so that you are able to easily InternalizeL.

    Hope this helps.

  10. #10
    Nokia Developer Expert
    Join Date
    Feb 2005
    Location
    India
    Posts
    209

    Re: Reading & Writing a file (uRGENT)

    Hi all,

    How can we read an ascii file of encoding type ANSI ?????? or how can we convert TBuf8 to TBuf<256> ???????

    Thanks,
    Anoop

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    18

    Re: Reading & Writing a file (uRGENT)

    Hi all,

    How can we convert file of encoding type ANSI to unicode file ??????

    Thanks,
    Aswathy

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Re: Reading & Writing a file (uRGENT)

    could you give me some help???
    I need example code about writing arbitrary binary data in the file
    thank you in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •