MFileTools

From MaratisWiki
Jump to: navigation, search

Collection of file manipulation tools, including File I/O designed to directly replace stdio, using MFile* in place of FILE*.

Contents

Code

Source.

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

see also:MFile::open, fopen

close

Close a file stream, if open.

see also:MFile::close, fclose

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:MFile::read, 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:MFile::write, 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:MFile::print, 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:MFile::seek, fseek

tell

Obtains the current value of the file position indicator for the file

see also:MFile::tell, ftell

rewind

Sets the file position indicator to the beginning of the file.

see also:MFile::rewind, rewind

Examples

MFile* fp = M_fopen("filename.txt", "r");
M_fseek(fp, 0L, SEEK_END);
long size = M_ftell(fp);
M_fseek(fp, 0L, SEEK_SET);
char* buffer = new char[size];
M_fread(buffer, 1, size, fp);
M_fclose(fp);
delete [] buffer;

Notes

Function descriptions adapted from man(3)

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox