EV3RT CXX API Reference [English]
An RTOS-based development platform for LEGO Mindstorms EV3.
ev3cxx_file.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <stdio.h>
10 
11 #include "ev3api.h"
12 #include "ev3cxx_format.h"
13 
14 namespace ev3cxx {
15 
20 class File
21 {
22 protected:
23  File() : m_filedesc(NULL) {}
24 
25 public:
34  File(const char * filename, const char * mode = "w+") {
35  open(filename, mode);
36  }
37 
42  virtual ~File() {
43  close();
44  }
45 
50  FILE * operator () () {
51  return m_filedesc;
52  }
53 
63  virtual bool_t open(const char * filename, const char * mode = "w+") {
64  return ((m_filedesc = fopen(filename, mode)) != NULL);
65  }
66 
72  virtual bool_t isOpen(){
73  return (m_filedesc != NULL);
74  }
75 
81  virtual int close() {
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  }
91 
97  int write(char ch) {
98  return fputc(ch, m_filedesc);
99  }
100 
109  int readChar() {
110  return fgetc(m_filedesc);
111  }
112 
122  template <typename T>
123  typename std::enable_if<std::is_integral<T>::value, int>::type
124  readNumber(T &val)
125  {
126  long long integral;
127  int er = fscanf(m_filedesc, "%lld", &integral);
128  val = integral;
129  return er;
130  }
131 
132  template <typename T>
133  typename std::enable_if<std::is_floating_point<T>::value, int>::type
134  readNumber(T &val)
135  {
136  double floating_point;
137  int er = fscanf(m_filedesc, "%lf", &floating_point);
138  val = floating_point;
139  return er;
140  }
141 
147  void rewind() {
149  }
150 
151 protected:
152  FILE * m_filedesc;
153 
154 }; // class File
155 
156 } // namespace ev3cxx
int write(char ch)
Write character to file.
Definition: ev3cxx_file.h:97
virtual bool_t isOpen()
Return if is file open.
Definition: ev3cxx_file.h:72
virtual bool_t open(const char *filename, const char *mode="w+")
Return file descriptor to open file or NULL.
Definition: ev3cxx_file.h:63
Definition: ev3cxx.h:35
File(const char *filename, const char *mode="w+")
Constructor of class File.
Definition: ev3cxx_file.h:34
FILE * m_filedesc
file of this class
Definition: ev3cxx_file.h:152
API for formating data to output (display, bluetooth, file)
int readChar()
Read character from file.
Definition: ev3cxx_file.h:109
std::enable_if< std::is_integral< T >::value, int >::type readNumber(T &val)
Read number from file.
Definition: ev3cxx_file.h:124
virtual ~File()
Destructor of class File. Close the file if is open.
Definition: ev3cxx_file.h:42
Class File. API for working with file.
Definition: ev3cxx_file.h:20
void rewind()
Set position of stream to the beginning.
Definition: ev3cxx_file.h:147
FILE * operator()()
Return file descriptor to open file or NULL.
Definition: ev3cxx_file.h:50
virtual int close()
Close the file if is open.
Definition: ev3cxx_file.h:81