class mrpt::io::CFileStream¶
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
#include <mrpt/io/CFileStream.h> class CFileStream: public mrpt::io::CStream { public: // construction CFileStream(const std::string& fileName, TFileOpenModes mode = fomRead|fomWrite); CFileStream(); CFileStream(const CFileStream&); // methods CFileStream& operator = (const CFileStream&); virtual size_t Read(void* Buffer, size_t Count); virtual size_t Write(const void* Buffer, size_t Count); bool open(const std::string& fileName, TFileOpenModes mode = fomRead|fomWrite); void close(); bool fileOpenCorrectly() const; bool is_open(); bool checkEOF(); void clearError(); virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning); virtual uint64_t getTotalBytesCount() const; virtual uint64_t getPosition() const; uint64_t getPositionI(); uint64_t getPositionO(); bool readLine(std::string& str); };
Inherited Members¶
public: // methods virtual size_t Read(void* Buffer, size_t Count) = 0; virtual size_t Write(const void* Buffer, size_t Count) = 0; virtual uint64_t getTotalBytesCount() const = 0; virtual uint64_t getPosition() const = 0;
Construction¶
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 conbination of different values. |
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.
Methods¶
virtual size_t Read(void* Buffer, size_t Count)
Introduces a pure virtual method responsible for reading from the stream.
virtual size_t Write(const void* Buffer, size_t Count)
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 conbination 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.
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 Offset, CStream::TSeekOrigin Origin = sFromBeginning)
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
Returns the total amount of bytes in the stream.
virtual uint64_t getPosition() const
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