Rtos API example

Embed: (wiki syntax)

« Back to documentation index

File Class Reference

File Class Reference
[Filesystem]

File class. More...

#include <File.h>

Inherits mbed::FileHandle.

Public Member Functions

 File ()
 Create an uninitialized file.
 File (FileSystem *fs, const char *path, int flags=O_RDONLY)
 Create a file on a filesystem.
virtual ~File ()
 Destroy a file.
virtual int open (FileSystem *fs, const char *path, int flags=O_RDONLY)
 Open a file on the filesystem.
virtual int close ()
 Close a file.
virtual ssize_t read (void *buffer, size_t size)
 Read the contents of a file into a buffer.
virtual ssize_t write (const void *buffer, size_t size)
 Write the contents of a buffer to a file.
virtual int sync ()
 Flush any buffers associated with the file.
virtual int isatty ()
 Check if the file in an interactive terminal device.
virtual off_t seek (off_t offset, int whence=SEEK_SET)
 Move the file position to a given offset from from a given location.
virtual off_t tell ()
 Get the file position of the file.
virtual void rewind ()
 Rewind the file position to the beginning of the file.
virtual off_t size ()
 Get the size of the file.
 MBED_DEPRECATED_SINCE ("mbed-os-5.4","Replaced by FileHandle::seek") virtual off_t lseek(off_t offset
 Move the file position to a given offset from a given location.

Detailed Description

File class.

Definition at line 30 of file File.h.


Constructor & Destructor Documentation

File (  )

Create an uninitialized file.

Must call open to initialize the file on a file system

File ( FileSystem fs,
const char *  path,
int  flags = O_RDONLY 
)

Create a file on a filesystem.

Creates and opens a file on a filesystem

Parameters:
fsFilesystem as target for the file
pathThe name of the file to open
flagsThe flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
virtual ~File (  ) [virtual]

Destroy a file.

Closes file if the file is still open


Member Function Documentation

virtual int close (  ) [virtual]

Close a file.

Returns:
0 on success, negative error code on failure

Implements FileHandle.

virtual int isatty (  ) [virtual]

Check if the file in an interactive terminal device.

Returns:
True if the file is a terminal

Reimplemented from FileHandle.

MBED_DEPRECATED_SINCE ( "mbed-os-5.4"  ,
"Replaced by FileHandle::seek"   
) [inherited]

Move the file position to a given offset from a given location.

Parameters:
offsetThe offset from whence to move to
whenceSEEK_SET for the start of the file, SEEK_CUR for the current file position, or SEEK_END for the end of the file.
Returns:
new file position on success, -1 on failure or unsupported
virtual int open ( FileSystem fs,
const char *  path,
int  flags = O_RDONLY 
) [virtual]

Open a file on the filesystem.

Parameters:
fsFilesystem as target for the file
pathThe name of the file to open
flagsThe flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR, bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
Returns:
0 on success, negative error code on failure
virtual ssize_t read ( void *  buffer,
size_t  size 
) [virtual]

Read the contents of a file into a buffer.

Parameters:
bufferThe buffer to read in to
sizeThe number of bytes to read
Returns:
The number of bytes read, 0 at end of file, negative error on failure

Implements FileHandle.

virtual void rewind (  ) [virtual]

Rewind the file position to the beginning of the file.

Note:
This is equivalent to file_seek(file, 0, FS_SEEK_SET)

Reimplemented from FileHandle.

virtual off_t seek ( off_t  offset,
int  whence = SEEK_SET 
) [virtual]

Move the file position to a given offset from from a given location.

Parameters:
offsetThe offset from whence to move to
whenceThe start of where to seek SEEK_SET to start from beginning of file, SEEK_CUR to start from current position in file, SEEK_END to start from end of file
Returns:
The new offset of the file

Implements FileHandle.

virtual off_t size (  ) [virtual]

Get the size of the file.

Returns:
Size of the file in bytes

Reimplemented from FileHandle.

virtual int sync (  ) [virtual]

Flush any buffers associated with the file.

Returns:
0 on success, negative error code on failure

Reimplemented from FileHandle.

virtual off_t tell (  ) [virtual]

Get the file position of the file.

Returns:
The current offset in the file

Reimplemented from FileHandle.

virtual ssize_t write ( const void *  buffer,
size_t  size 
) [virtual]

Write the contents of a buffer to a file.

Parameters:
bufferThe buffer to write from
sizeThe number of bytes to write
Returns:
The number of bytes written, negative error on failure

Implements FileHandle.