MFile
(→Code) |
|||
Line 2: | Line 2: | ||
==Code== | ==Code== | ||
+ | [http://code.google.com/p/maratis/source/browse/trunk/dev/MSDK/MCore/Includes/MFile.h Source.] | ||
<pre> | <pre> | ||
class M_CORE_EXPORT MFile | class M_CORE_EXPORT MFile | ||
Line 39: | Line 40: | ||
| write only, erase existing file and start at the beginning | | write only, erase existing file and start at the beginning | ||
|} | |} | ||
+ | |||
+ | ''Note: This usually should not be used directly and should be opened from the [[MFileTools]] interface'' | ||
see also:[[MFileTools#M_fopen|M_fopen]], [http://www.cplusplus.com/reference/clibrary/cstdio/fopen/ fopen] | see also:[[MFileTools#M_fopen|M_fopen]], [http://www.cplusplus.com/reference/clibrary/cstdio/fopen/ fopen] | ||
Line 48: | Line 51: | ||
===read=== | ===read=== | ||
+ | Reads ''count'' amount of elements of data, each ''size'' bytes long, from the file, storing them at the location given by ''dest''. | ||
+ | Returns the number of items successfully read. If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero) | ||
see also:[[MFileTools#M_fread|M_fread]], [http://www.cplusplus.com/reference/clibrary/cstdio/fread/ fread] | see also:[[MFileTools#M_fread|M_fread]], [http://www.cplusplus.com/reference/clibrary/cstdio/fread/ fread] | ||
+ | |||
+ | ===write=== | ||
+ | Writes ''count'' amount of elements of data, each ''size'' bytes long, to the file, obtaining them from the location given by ''str''. | ||
+ | Returns the number of items successfully written. | ||
+ | |||
+ | see also:[[MFileTools#M_fwrite|M_fwrite]], [http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/ fwrite] | ||
+ | |||
+ | ===print=== | ||
+ | Produces output according to the standard printf() style ''format'' and writes to the file. | ||
+ | Returns the number of characters written to the file (excluding the null byte used to end output strings) | ||
+ | |||
+ | see also:[[MFileTools#M_fprintf|M_fprintf]], [http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/ fprintf] | ||
+ | |||
+ | ===seek=== | ||
+ | Sets the file position indicator. The new position, measured in bytes, is obtained by adding ''offset'' bytes to the position specified by ''whence''. If ''whence'' is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file respectively. | ||
+ | |||
+ | see also:[[MFileTools#M_fseek|M_fseek]], [http://www.cplusplus.com/reference/clibrary/cstdio/fseek/ fseek] | ||
+ | |||
+ | ===tell=== | ||
+ | Obtains the current value of the file position indicator for the file | ||
+ | |||
+ | see also:[[MFileTools#M_ftell|M_ftell]], [http://www.cplusplus.com/reference/clibrary/cstdio/ftell/ ftell] | ||
+ | |||
+ | ===rewind=== | ||
+ | Sets the file position indicator to the beginning of the file. | ||
+ | |||
+ | see also:[[MFileTools#M_rewind|M_rewind]], [http://www.cplusplus.com/reference/clibrary/cstdio/rewind/ rewind] | ||
+ | |||
+ | ==Examples== | ||
+ | ''Note: Usually the file interface won't be used directly. See:[[MFileTools]]'' | ||
+ | |||
+ | <pre> | ||
+ | MFile* fp = M_fopen("filename.txt", "r"); | ||
+ | fp->seek(0L, SEEK_END); | ||
+ | long size = fp->tell(); | ||
+ | char* buffer = new char[size]; | ||
+ | fp->rewind(); | ||
+ | fp->read(buffer, 1, size); | ||
+ | fp->close(); | ||
+ | fp->destroy(); // if we're not using the MFileTools interface, we need to manually destroy | ||
+ | delete [] buffer; | ||
+ | </pre> |
Revision as of 14:37, 19 April 2012
File I/O class. This interface can be used manually, but will usually be used from the MFileTools interface, to more closely match standard C file I/O.
Contents |
Code
class M_CORE_EXPORT MFile { public: // destructor virtual ~MFile(void){} virtual void open(const char* path, const char* mode) = 0; virtual int close() = 0; virtual size_t read(void* dest, size_t size, size_t count) = 0; virtual size_t write(const void* str, size_t size, size_t count) = 0; virtual int print(const char* format, ...) = 0; virtual int print(const char* format, va_list args) = 0; virtual int seek(long offset, int whence) = 0; virtual long tell() = 0; virtual void rewind() = 0; virtual bool isOpen() = 0; virtual void destroy() = 0; };
API
open
Opens a file stream with the name specified by path. The argument mode should be a string containing one of the following modes:
mode | description |
---|---|
"r" | read only, start at the beginning of the file |
"w" | write only, erase existing file and start at the beginning |
Note: This usually should not be used directly and should be opened from the MFileTools interface
close
Close a file stream, if open.
read
Reads count amount of elements of data, each size bytes long, from the file, storing them at the location given by dest. Returns the number of items successfully read. If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero)
write
Writes count amount of elements of data, each size bytes long, to the file, obtaining them from the location given by str. Returns the number of items successfully written.
Produces output according to the standard printf() style format and writes to the file. Returns the number of characters written to the file (excluding the null byte used to end output strings)
seek
Sets the file position indicator. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file respectively.
tell
Obtains the current value of the file position indicator for the file
rewind
Sets the file position indicator to the beginning of the file.
Examples
Note: Usually the file interface won't be used directly. See:MFileTools
MFile* fp = M_fopen("filename.txt", "r"); fp->seek(0L, SEEK_END); long size = fp->tell(); char* buffer = new char[size]; fp->rewind(); fp->read(buffer, 1, size); fp->close(); fp->destroy(); // if we're not using the MFileTools interface, we need to manually destroy delete [] buffer;