Reference » Service classes » MFile

Elgrint::MFile class

Declared in MFile.h

Description

Service class for creating, editing, and reading binary files or data streams.
Read more...

Initializers:

Name Description
MFile() Default constructor. Creates an object not associated with any file.
MFile(pathname,fm) Creates an object and associates it with the specified file.
~MFile() Destructor. Closes the associated file (if open) and destroys the object.

Mutators:

Name Description
close() Closes the associated file and resets all internal data (except for getPathname).
open(pathname,fm) Associates the object with the specified file/stream and opens it for reading and/or editing.
operator<<<T>(src) Writes all the T-type elements of the specified vector to the file, and advances the file pointer.
operator<<(src) Writes a single element of type T (see Details) to the file, and advances the file pointer accordingly.
operator>><T>(dest) Reads up to dest.cnt() elements of type T from the file and advances the file pointer.
operator>>(dest) Reads a single element of type T (see Details) from the file and advances the file pointer accordingly.
setPos(newPos) Changes the current position of a local file pointer (doesn't work on online files).
truncate(cnt) Truncates an open local by cnt bytes (works only on local files).

Revealers:

Name Description
getPathname() Returns the canonic pathname of the file name that was specified during the last open operation.
getPos() Returns the current position of the file pointer.
getSize() Returns the current size of the open file in bytes.
isEOF() Returns true iff the current position is at the End-Of-File.
isOpen() Returns true iff the associated file is open (see open).

Details

MFile object can read and write variables of various types, and can be easily expanded to any other type by overloading operator<< (writing) and operator>> (reading).

'MFile' object works with local files, FTP files, and HTTP/HTTPS streams (which are not really files, even if they sometimes correspond to the actual files on the server). Note that for online files the functionality is more limited than for local files: non-trivial setPos and truncate work only with local files, and reading is allowed only in fmRead mode.

Each written variable is encoded in a platform-independent way, so that the resulting file may be copied to any other platform and an Elgrint app on that platform would be able to read all the data properly. However, the data is encoded in a way that non-Elgrint apps may not be able to understand. The only type that is guaranteed to be universally understood is UInt8 (byte). See more about this in operator<< and operator>>.

'MFile' functions use exceptions from 2000 through 2007 to indicate all possible file-related errors. As a rule, the greater the exception's code, the more severe the error is, with 2007 (the worst one) being the only error that can indicate the file content corruption. The file is automatically closed on any error (unless it was already closed, of course), so isOpen can be used as a programmatic error indicator in addition to the exceptions.

Remarks

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.