Declared in MFile.h
Service class for creating, editing, and reading binary files or data streams.
|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.|
|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).|
|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).|
'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.
- MFile treats soft links (.lnk files in Windows) as regular files. In other words, calling open on a link file opens the link file itself and not the file to which that this points. This is unlike MSys::open, which follows the specified link and opens the linked file
- HTTP does not define a file system, so an open HTTP (or HTTPS) stream does not necessarily correspond to a real file on the server, and the size of the stream is generally unknown. See more on this in open, getSize, and operator<<
- MFile object cannot be copied or assigned
- MFile functions may not be used outside of the MAppMain scope (i.e. in static or global scope), except for MFile, ~MFile, and close. In other words, MFile objects can be defined globally, but used only within the scope of MAppMain. All files should be closed before MAppMain returns
Let us know
Please Contact us to report any errors on this page, or to suggest any improvements.