EV3RT CXX API Reference [English]
An RTOS-based development platform for LEGO Mindstorms EV3.
File Class Reference

Class File. API for working with file. More...

#include <ev3cxx_file.h>

Inheritance diagram for File:
Bluetooth

Public Member Functions

 File (const char *filename, const char *mode="w+")
 Constructor of class File. More...
 
virtual ~File ()
 Destructor of class File. Close the file if is open.
 
FILE * operator() ()
 Return file descriptor to open file or NULL.
 
virtual bool_t open (const char *filename, const char *mode="w+")
 Return file descriptor to open file or NULL. More...
 
virtual bool_t isOpen ()
 Return if is file open. More...
 
virtual int close ()
 Close the file if is open. More...
 
int write (char ch)
 Write character to file. More...
 
int readChar ()
 Read character from file. More...
 
template<typename T >
std::enable_if< std::is_integral< T >::value, int >::type readNumber (T &val)
 Read number from file. More...
 
template<typename T >
std::enable_if< std::is_floating_point< T >::value, int >::type readNumber (T &val)
 
void rewind ()
 Set position of stream to the beginning. More...
 

Protected Attributes

FILE * m_filedesc
 file of this class
 

Detailed Description

Class File. API for working with file.

Definition at line 20 of file ev3cxx_file.h.

Constructor & Destructor Documentation

◆ File()

File ( const char *  filename,
const char *  mode = "w+" 
)
inline

Constructor of class File.

Parameters
filenamePath to file.
modeMode in which will be file open. More info http://www.cplusplus.com/reference/cstdio/fopen/ Default mode is "w+": Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

Definition at line 34 of file ev3cxx_file.h.

References File::open().

34  {
35  open(filename, mode);
36  }
virtual bool_t open(const char *filename, const char *mode="w+")
Return file descriptor to open file or NULL.
Definition: ev3cxx_file.h:63

Member Function Documentation

◆ close()

virtual int close ( )
inlinevirtual

Close the file if is open.

Returns
If the file is successfully closed, a zero value is returned. On failure, EOF is returned.

Reimplemented in Bluetooth.

Definition at line 81 of file ev3cxx_file.h.

References File::isOpen(), and File::m_filedesc.

Referenced by File::~File().

81  {
82  if(isOpen()) {
83  int ret = fclose(m_filedesc);
84  if(0 == ret) {
85  m_filedesc = NULL;
86  }
87  return ret;
88  }
89  return 0;
90  }
virtual bool_t isOpen()
Return if is file open.
Definition: ev3cxx_file.h:72
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152

◆ isOpen()

virtual bool_t isOpen ( )
inlinevirtual

Return if is file open.

Returns
If the file is open return true, else false.

Reimplemented in Bluetooth.

Definition at line 72 of file ev3cxx_file.h.

References File::m_filedesc.

Referenced by File::close().

72  {
73  return (m_filedesc != NULL);
74  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152

◆ open()

virtual bool_t open ( const char *  filename,
const char *  mode = "w+" 
)
inlinevirtual

Return file descriptor to open file or NULL.

Parameters
filenamePath to file.
modeMode in which will be file open. More info http://www.cplusplus.com/reference/cstdio/fopen/ Default mode is "w+": Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
Returns
Return true if is open, else false.

Reimplemented in Bluetooth.

Definition at line 63 of file ev3cxx_file.h.

References File::m_filedesc.

Referenced by File::File().

63  {
64  return ((m_filedesc = fopen(filename, mode)) != NULL);
65  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152

◆ readChar()

int readChar ( )
inline

Read character from file.

Returns
On success, the character read is returned (promoted to an int value). The return type is int to accommodate for the special value EOF, which indicates failure: If the position indicator was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stream. If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead.

Definition at line 109 of file ev3cxx_file.h.

References File::m_filedesc.

109  {
110  return fgetc(m_filedesc);
111  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152

◆ readNumber()

std::enable_if<std::is_integral<T>::value, int>::type readNumber ( T &  val)
inline

Read number from file.

Returns
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file. If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

Definition at line 124 of file ev3cxx_file.h.

References File::m_filedesc.

125  {
126  long long integral;
127  int er = fscanf(m_filedesc, "%lld", &integral);
128  val = integral;
129  return er;
130  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152

◆ rewind()

void rewind ( )
inline

Set position of stream to the beginning.

Vraper around standard function rewind(). Sets the position indicator associated with stream to the beginning of the file.

Definition at line 147 of file ev3cxx_file.h.

References File::m_filedesc.

147  {
149  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152
void rewind()
Set position of stream to the beginning.
Definition: ev3cxx_file.h:147

◆ write()

int write ( char  ch)
inline

Write character to file.

Returns
Return the character written as an unsigned char cast to an int or EOF on error.

Definition at line 97 of file ev3cxx_file.h.

References File::m_filedesc.

97  {
98  return fputc(ch, m_filedesc);
99  }
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152