SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
File Class Reference

A class for file reading and writing. More...

#include <File.h>

Public Member Functions

 File ()
 Constructor. More...
 
 File (const char *str, const char *mode)
 Constructor. More...
 
 ~File ()
 Desctructor. More...
 
bool open (const char *str, const char *mode)
 Open the file. More...
 
size_t print (const char *str)
 Print a string. More...
 
size_t write (void *buf, size_t len)
 Write a buffer. More...
 
size_t read (void *buf, size_t len)
 Read a buffer. More...
 
size_t read (unsigned char **p_buf)
 Read all the file. More...
 
bool gets (char *str, size_t len)
 Get a string. More...
 
void close ()
 Clone the file. More...
 
bool isOpen ()
 Check if open. More...
 
bool eof ()
 Check end-of-file. More...
 
 operator bool ()
 

Private Attributes

FILE * p_file
 Pointer to the C-type file. More...
 
bool _isOpen
 true if file is open More...
 

Detailed Description

A class for file reading and writing.

Class for file reading and writing wroking with Windows and Linux. Actually this is just a wrapper for the FILE type in the stdio C library.

Author
Stefano Lovato
Date
2022

Constructor & Destructor Documentation

◆ File() [1/2]

File::File ( )

Constructor.

Default constructor. File is not open.

◆ File() [2/2]

File::File ( const char *  str,
const char *  mode 
)

Constructor.

Constructor for the file class with opening.

Parameters
strThe name of the file.
modeThe mode for the file opening. Refer to the stdio library for the meaning.

◆ ~File()

File::~File ( )

Desctructor.

Default destructor. No parameter is necessary. It automatically closes the file if open.

Member Function Documentation

◆ close()

void File::close ( )

Clone the file.

Function to clone the file.

◆ eof()

bool File::eof ( )

Check end-of-file.

Function to check if the end-of-file is reached.

Returns
true if the end of file is reached, false otherwise.

◆ gets()

bool File::gets ( char *  str,
size_t  len 
)

Get a string.

Function to get a string to the file.

Parameters
strThe string used to store the char read.
lenThe length of the string to read.
Returns
true if success, false otherwise.

◆ isOpen()

bool File::isOpen ( )
inline

Check if open.

Function to check if the file is open.

Returns
true if the file is open, false otherwise

◆ open()

bool File::open ( const char *  str,
const char *  mode 
)

Open the file.

Function to open the file.

Parameters
strThe name of the file.
modeThe mode for the file opening. Refer to the stdio library for the meaning.
Returns
true if success, false otherwise.

◆ operator bool()

File::operator bool ( )
inline

◆ print()

size_t File::print ( const char *  str)

Print a string.

Function to print a string to the file.

Parameters
strThe string to print.
Returns
The number of written char (equal to the string length if success).

◆ read() [1/2]

size_t File::read ( unsigned char **  p_buf)

Read all the file.

Function to read all the file into a buffer. Memory is automatically allocated according to the file size.

Parameters
p_bufThe pointer of the pointer to the buffer.
Returns
The number of read char (equal to the file size if sucess).

◆ read() [2/2]

size_t File::read ( void *  buf,
size_t  len 
)

Read a buffer.

Function to read a buffer to the file.

Parameters
bufThe pointer to the buffer.
lenThe length of the buffer in bytes.
Returns
The number of read char (equal to the buffer length if success).

◆ write()

size_t File::write ( void *  buf,
size_t  len 
)

Write a buffer.

Function to write a buffer to the file.

Parameters
bufThe pointer to the buffer.
lenThe length of the buffer in bytes.
Returns
The number of written char (equal to the buffer length if success).

Member Data Documentation

◆ _isOpen

bool File::_isOpen
private

true if file is open

◆ p_file

FILE* File::p_file
private

Pointer to the C-type file.