MRPT  2.0.1
io/CFileOutputStream.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/io/CStream.h>
12 #include <fstream>
13 
14 namespace mrpt::io
15 {
16 /** This CStream derived class allow using a file as a write-only, binary
17  * stream.
18  *
19  * \sa CStream, CFileStream, CFileGZOutputStream
20  * \ingroup mrpt_io_grp
21  */
22 class CFileOutputStream : public CStream
23 {
24  private:
25  /** The actual output file stream. */
26  std::ofstream m_of;
27 
28  public:
29  /** Constructor
30  * \param fileName The file to be open in this stream
31  * \param append If set to true, the file will be opened for writing and the
32  * current cursor position set at the end of the file. Otherwise, previous
33  * contents will be lost.
34  * \exception std::exception if the file cannot be opened.
35  */
36  CFileOutputStream(const std::string& fileName, bool append = false);
37 
38  /** Default constructor */
40 
41  CFileOutputStream(const CFileOutputStream&) = delete;
43 
44  /** Open the given file for write
45  * \param fileName The file to be open in this stream
46  * \param append If set to true, the file will be opened for writing and the
47  * current cursor position set at the end of the file. Otherwise, previous
48  * contents will be lost.
49  * \sa fileOpenCorrectly
50  * \return true on success.
51  */
52  bool open(const std::string& fileName, bool append = false);
53 
54  /** Close the stream. */
55  void close();
56 
57  /** Destructor */
58  ~CFileOutputStream() override;
59 
60  /** Returns true if the file was open without errors. */
61  bool fileOpenCorrectly() const;
62  /** Returns true if the file was open without errors. */
63  bool is_open() { return fileOpenCorrectly(); }
64  // See base class docs
65  uint64_t Seek(
66  int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override;
67 
68  /** Method for getting the total number of bytes writen to buffer */
69  uint64_t getTotalBytesCount() const override;
70 
71  /** Method for getting the current cursor position, where 0 is the first
72  * byte and TotalBytesCount-1 the last one */
73  uint64_t getPosition() const override;
74 
75  size_t Read(void* Buffer, size_t Count) override;
76  size_t Write(const void* Buffer, size_t Count) override;
77 }; // End of class def.
78 static_assert(
79  !std::is_copy_constructible_v<CFileOutputStream> &&
80  !std::is_copy_assignable_v<CFileOutputStream>,
81  "Copy Check");
82 } // namespace mrpt::io
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
CFileOutputStream()
Default constructor.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
bool is_open()
Returns true if the file was open without errors.
std::ofstream m_of
The actual output file stream.
This CStream derived class allow using a file as a write-only, binary stream.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
CFileOutputStream & operator=(const CFileOutputStream &)=delete
~CFileOutputStream() override
Destructor.
uint64_t getTotalBytesCount() const override
Method for getting the total number of bytes writen to buffer.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
void close()
Close the stream.



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020