Class CFileStream

Inheritance Relationships

Base Type

Class Documentation

class CFileStream : public mrpt::io::CStream

This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn’t exist. The default behavior can be change to open as read, write, read and write,… in the constructor.

See also

CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream

Public Functions

CFileStream(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite)

Constructor and open a file

Parameters:
  • fileName – The file to be open in this stream

  • mode – The open mode: can be an or’d combination of different values.

Throws:

std::exception – On error creating or accessing the file. By default the file is opened for open and write and created if not found.

CFileStream()

Constructor

CFileStream(const CFileStream&) = delete
CFileStream &operator=(const CFileStream&) = delete
~CFileStream() override

Destructor

virtual size_t Read(void *Buffer, size_t Count) override

Introduces a pure virtual method responsible for reading from the stream.

virtual size_t Write(const void *Buffer, size_t Count) override

Introduces a pure virtual method responsible for writing to the stream. Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.

bool open(const std::string &fileName, TFileOpenModes mode = fomRead | fomWrite)

Opens the file, returning true on success.

Parameters:
  • fileName – The file to be open in this stream

  • mode – The open mode: can be an or’d combination of different values. By default the file is opened for open and write and created if not found.

void close()

Closes the file

bool fileOpenCorrectly() const

Returns true if the file was open without errors.

inline bool is_open()

Returns true if the file was open without errors.

bool checkEOF()

Will be true if EOF has been already reached.

void clearError()

Resets stream error status bits (e.g. after an EOF)

virtual uint64_t Seek(int64_t off, CStream::TSeekOrigin org = sFromBeginning) override

Introduces a pure virtual method for moving to a specified position in the streamed resource. he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:

  • sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.

  • sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.

  • sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.

Returns:

Seek returns the new value of the Position property.

virtual uint64_t getTotalBytesCount() const override

Returns the total amount of bytes in the stream.

virtual uint64_t getPosition() const override

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

uint64_t getPositionI()

The current Input cursor position, where 0 is the first byte

uint64_t getPositionO()

The current Input cursor position, where 0 is the first byte

bool readLine(std::string &str)

Reads one string line from the file (until a new-line character)

Returns:

true if a line has been read, false on EOF or error