Reference » Service classes » MFile » operator>>

Elgrint::MFile::operator>>(T&) method

Declaration (see MFile.h):

void operator>>(T& dest);


Reads a single element of type T (see Details) from the file and advances the file pointer accordingly.


Name Type Description
dest T&

A variable which contains the result of the reading when the function returns (a reading buffer).


By default, T can be any of the following types: bool, char, MChar, Int8, UInt8, Int8, Int16, UInt16, Int32, UInt32, Int64, UInt64, float, double, MNum, MFileSize, MLocusElem, MSizeElem. MPointElem, and MString.

However, operator>> can be overloaded to any other type using the predefined operator>> implementations (see Example).

If the file is closed, operator>> generates exception 2001 and returns. That being said, if the file was closed due to error, operator>> returns without generating an exception to avoid redundant error reports. If the file is an online file, which was opened in fmOverwrite mode (recall that online files cannot be opened in fmAppend mode), the function generates exception 2001 as well. Online files can be read only in fmRead mode (unlike local files that can be read in any mode).

Otherwise (if none of the above error conditions are met), the function reads data from the associated file into the dest variable, starting from the current position (see getPos). The position of the file pointer is advanced to the first byte after the end of the read data. The amount of data depends on the type of dest. For example, for UInt8 the data is just one byte, but for a more complex type like MString it can even be of variable length, depending on the stored value.

If EOF (End-Of-File) is reached before enough data is read, exception 2001 is generated. getPos and getSize can be used to prevent this error, unless the file size is not known in advance (e.g. in HTTP stream). The exception 2001 can also be generated if the reading operation cannot parse the read data, even if the reading itself was successful. Such an error indicates that the file is corrupt or is being read incorrectly. If there was an I/O error, then exception 2006 is generated.

On any error, the file is closed and dest is reset to "zero" (0, 0.0, false, ZeroChar, or ZeroString, depending on T) to prevent an undefined result.

See MFile for (another) usage example.



Let us know

Please Contact us to report any errors on this page, or to suggest any improvements.

Miranor Home | About Miranor | About Elgrint | Create account | Login | Account settings | Contact Us | Privacy Policy | Site map

© Copyright 2014 by Miranor. All rights reserved. By using this site you agree to the Terms of Use.

Page last updated on August 10th, 2014.