File System Development-file stream operations

Source: Internet
Author: User
Tags stack pop

 

Stream read/write operations are implemented through the base class rreadstream class and rwritestream class. For example, the commitl () function is used for final data writing, the writel () function is used for external data, and the readl () function is used for internal data. There are different Derived classes based on different media. The Derived classes for reading and writing file streams are rfilereadstream and rwritefilestream. When writing a file stream, you must use the rfilewritestream: Create () function to create a file. When reading data from the file stream, you must use the rfilereadstrea: open () function to open the file.

 

Most file operation functions in Symbian OS do not exit abnormally, but return an error value. Therefore, when a program is affected by an operation error, use the user: leaveiferror () function to forcibly exit the program with an exception. When pushing the objects of the rwritestream class and its derived classes to the clear stack, do not use the pushl () function of the clear stack, but use the pushl () function of the objects of this class. Use rfilewritestream to write a tint16 value to the stream. The reference code is as follows:

 

Void writetostreamfilel (RFS & AFS, tdesc & afilename, tint16 * aint)

 

{

 

Rfilewritestream write;

 

// Use rwritestream: pushl () instead of clearing the pushl () of the stack ()

 

Writer. pushl ();

 

User: leaveiferror (writer. Create (AFS, afilename, efilewriter ));

 

Writer <* aint;

 

// Data cannot be rollback after commitl ()

 

Writer. commitl ();

 

// Use rwritestream: Pop () instead of clearing the stack POP ()

 

Writer. Pop ();

 

// Use the release function to release rwritestream

 

Writer. Release ();

 

}

 

When clearing the stack, you can use the POP () function of the object, and then use the release () function to release the object of the rwritestream class. You can also use the popanddestroy () function of the clear stack to pop up and destroy this object. This rule applies to the rreadstream class. The code for reading a tint16 value from the stream using the rreadstream class is as follows:

 

Void readfromstreamfilel (RFS & AFS, tdesc & afilename, tint16 * aint)

 

{

 

Rfilereadstream reader;

 

// Press the rreadstream: pushl function to clear the stack

 

Reader. pushl ();

 

User: leaveiferror (reader. Open (AFS, afilename, efileread ));

 

Reader> * aint;

 

// Use the function of clearing the stack to pop up and destroy the rfilereadstream object.

 

// The statement has the same scope {reader. Pop (); reader. Release ();}

 

Cleanupstack: popanddestroy (); // Reader

 

}

 

The preceding Code uses the operator <complete data externalization, use> to complete data internalization.

 

To ensure that the stream sequence is not affected, make sure that the length and sequence of the specified operands are the same during read/write. For example:

 

Tint I = 1234;

 

Tint32 J = 5678;

 

Writer. writeint32l (I );

 

Writer <J;

 

....

 

I = reader. readint32l (I );

 

Reader> J;

 

When you use the operator <to externalize a string to a stream, the string is no longer required to be 8 bits, nor to limit the length of the string. The operator <will record this information in the stream, to ensure that the content of the string can be correctly read during internalization.

 

_ Partition (ksampletext, "Hello World ");

 

Tbuf <20> text = ksampletext;

 

Writer <text;

 

...

 

Tbuf <20> text2;

 

Reader> text2;

 

For a class of any type, such as a class of the "T" type, a class of the "C" type or an independent class, the externalizel () function and internalizel () function must be implemented, then, you can use the operators "<" and ">" to write and read the data in the class object to the stream. For example:

 

Class anyclass

 

{

 

Public:

 

...

 

Void externalizel (rwritesteam & astream)

 

{

 

Asteam <itext;

 

Astream <iint;

 

Astream <ireal;

 

}

 

Void interlizel (rreadstream & astream)

 

 

{

 

Asteam> itext;

 

Astream> iint;

 

Astream> ireal;

 

}

 

PRIVATE:

 

Tbuf <32> itext;

 

Tint32 iint;

 

Treal64 ireal;

 

}

 

When externalizing and internalizing anyclass objects, you can use the operators "<" and ">"

 

Anyclass object;

 

Writer <object;

 

...

 

Anyclass object2;

 

Reader> object2;

 

Or you can use the externalizel () function and internalize () function directly, with the same running effect.

 

Anyclass object;

 

Object. externalizel (writer );

 

...

 

Anyclass object2;

 

Object2.internalizel (Reader );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.