File Class
Inherits: | Stream |
This class allows you to write and read data into files. Files are stored locally (unlike Buffer which exists only in the memory).
File Class
Members
static File()
Default constructor.static File(string name, string mode)
Constructor.Arguments
- string name - File name.
- string mode - Access mode (see open()).
string getName()
Returns a name of the opened file.Return value
File name.int getSize()
Returns the size of the opened file in bytes.Return value
File size in bytes.void close()
Flushes the stream (writing any buffered output data by flush()) and close the file descriptor.Return value
int eof()
Tests for end-of-file on a file descriptor.Return value
1 if it is the end of file; 0 otherwise.int flush()
Forces to write of all buffered data to the file.Return value
1 if the data is written successfully; otherwise, 0.int open(string name, string mode)
Opens a file with a given access mode: rb to open file for reading; wb to create a new file.- r - Open for reading only. The stream is positioned at the beginning of the file.
- r+ - Open for reading and writing. The stream is positioned at the beginning of the file.
- w - Truncate file to zero length or create file for writing. The stream is positioned at the beginning of the file.
- w+ - Open for reading and writing. The file is created if it does not exist, otherwise it is truncated to zero length. The stream is positioned at the beginning of the file.
- a - Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
- a+ - Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. The mode string can also include the letter b either as a last character or as a character between the characters in any of the two-character strings described above. It shall have no effect, but is allowed for ISO C standard conformance.
Arguments
- string name - Name of the file to open.
- string mode - Access mode.
Return value
Returns 1 if the file is opened (or created, if the wb access mode is specified) successfully; otherwise, 0.int seekCur(int offset)
Sets an offset of the file position indicator relative to its current position.Arguments
- int offset - Offset of the file position indicator from the current position, in bytes.
Return value
Returns 1 if the file position indicator offset is set successfully; otherwise, 0.int seekEnd(int offset)
Sets an offset of the file position indicator relative to the end of the file.Arguments
- int offset - File position indicator offset from the end of the file in bytes.
Return value
Returns 1 if the file position indicator offset is set successfully; otherwise, 0.int seekSet(int offset)
Sets an offset of the file position indicator relatively to the start of the file.Arguments
- int offset - Offset of the file position indicator from the start of the file, in bytes.
Return value
1 if the offset has been set successfully; otherwise, 0.int tell()
Returns the current offset of the file position indicator.Return value
Offset in bytes from the beginning of the file.Last update: 2018-08-10
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)