xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
List of all members
xtd::io::binary_writer Class Reference

#include <binary_writer.h>

Definition

Writes primitive types in binary to a stream and supports writing strings.

class core_export_ binary_writer : public xtd::object
Inheritance
xtd::objectxtd::io::binary_writer
Namespace
xtd::io
Library
xtd.core
Examples
The following code example demonstrates how to store and retrieve application settings in a file.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring file_name = "app_settings.dat";
public:
static void main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(fstream fs = file::open(file_name, ios::out | ios::binary | ios::trunc)) {
binary_writer writer(fs);
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
float aspect_ratio;
ustring temp_directory;
int auto_save_time;
bool show_status_bar;
if (file::exists(file_name)) {
using_(fstream fs = file::open(file_name, ios::in | ios::binary)) {
binary_reader reader(fs);
aspect_ratio = reader.read_single();
temp_directory = reader.read_string();
auto_save_time = reader.read_int32();
show_status_bar = reader.read_boolean();
}
console::write_line("Aspect ratio set to: {}", aspect_ratio);
console::write_line("Temp directory is: {}", temp_directory);
console::write_line("Auto save time set to: {}", auto_save_time);
console::write_line("Show status bar: {}", show_status_bar);
}
}
};
startup_(program);
// This code produces the following output :
//
// Aspect ratio set to: 1.25
// Temp directory is: c:\Temp
// Auto save time set to: 10
// Show status bar: true
Examples
The same example with stream operators.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring file_name = "app_settings.dat";
public:
static void main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(fstream fs = file::open(file_name, ios::out | ios::binary | ios::trunc)) {
binary_writer writer(fs);
writer << 1.250F;
writer << R"(c:\Temp)";
writer << 10;
writer << true;
}
}
static void display_values() {
float aspect_ratio;
ustring temp_directory;
int auto_save_time;
bool show_status_bar;
if (file::exists(file_name)) {
using_(fstream fs = file::open(file_name, ios::in | ios::binary)) {
binary_reader reader(fs);
reader >> aspect_ratio;
reader >> temp_directory;
reader >> auto_save_time;
reader >> show_status_bar;
}
console::write_line("Aspect ratio set to: {}", aspect_ratio);
console::write_line("Temp directory is: {}", temp_directory);
console::write_line("Auto save time set to: {}", auto_save_time);
console::write_line("Show status bar: {}", show_status_bar);
}
}
};
startup_(program);
// This code produces the following output :
//
// Aspect ratio set to: 1.25
// Temp directory is: c:\Temp
// Auto save time set to: 10
// Show status bar: true
Remarks
The xtd::io::binary_writer class provides methods that simplify writing primitive data types to a stream. For example, you can use the Write method to write a Boolean value to the stream as a one-byte value. The class includes write methods that support different data types.
When you create a new instance of the xtd::io::binary_writer class, you provide the stream to write to.

Constructors

 binary_writer (const xtd::ustring &path)
 Initializes a new instance of the binary_writer class for the specified file name. More...
 
 binary_writer (std::ostream &stream)
 Initializes a new instance of the binary_writer class for the specified stream. More...
 

Properties

std::optional< std::reference_wrapper< std::ostream > > base_stream () const
 Returns the underlying stream. More...
 

Methods

virtual void close ()
 Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources associated with the reader. More...
 
virtual void flush ()
 Clears all buffers for the current writer and causes any buffered data to be written to the underlying device. More...
 
virtual size_t seek (size_t offset, std::ios::seekdir origin)
 Sets the position within the current stream. More...
 
virtual void write (bool value)
 Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing true. More...
 
virtual void write (byte_t value)
 Writes an unsigned byte to the current stream and advances the stream position by one byte. More...
 
virtual void write (char value)
 Writes a character to the current stream and advances the current position by one byte. More...
 
template<size_t size>
void write (const std::array< byte_t, size > &buffer)
 Writes a byte array to the underlying stream. More...
 
template<size_t size>
void write (const std::array< char, size > &buffer)
 Writes a character array to the underlying stream. More...
 
virtual void write (const std::vector< byte_t > &buffer)
 Writes a byte array to the underlying stream. More...
 
virtual void write (const std::vector< byte_t > &buffer, size_t index, size_t count)
 Writes a region of a byte array to the current stream. More...
 
virtual void write (const std::vector< char > &buffer)
 Writes a character array to the underlying stream. More...
 
virtual void write (const std::vector< char > &buffer, size_t index, size_t count)
 Writes a region of a character array to the current stream. More...
 
virtual void write (double value)
 Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes. More...
 
virtual void write (int16_t value)
 Writes a two-byte signed integer to the current stream and advances the stream position by two bytes. More...
 
virtual void write (int32_t value)
 Writes a four-byte signed integer to the current stream and advances the stream position by four bytes. More...
 
virtual void write (int64_t value)
 Writes a eight-byte signed integer to the current stream and advances the stream position by eight bytes. More...
 
virtual void write (sbyte_t value)
 Writes an signed byte to the current stream and advances the stream position by one byte. More...
 
virtual void write (float value)
 Writes an four-byte floating-point value to the current stream and advances the stream position by four bytes. More...
 
virtual void write (const ustring &value)
 Writes a length-prefixed string to this stream, and advances the current position of the stream and the specific characters being written to the stream. More...
 
virtual void write (uint16_t value)
 Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes. More...
 
virtual void write (uint32_t value)
 Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes. More...
 
virtual void write (uint64_t value)
 Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight bytes. More...
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance. More...
 
virtual xtd::ustring to_string () const noexcept
 Returns a std::string that represents the current object. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 

Constructor & Destructor Documentation

◆ binary_writer() [1/2]

xtd::io::binary_writer::binary_writer ( const xtd::ustring path)
explicit

Initializes a new instance of the binary_writer class for the specified file name.

Parameters
pathThe complete file path to be read.
Exceptions
xtd::argument_exceptionpath contains one or more of the invalid characters
-or-
The system could not retrieve the absolute path.
xtd::io::io_exceptionthe handle of the specified file cannot be opened

◆ binary_writer() [2/2]

xtd::io::binary_writer::binary_writer ( std::ostream &  stream)
explicit

Initializes a new instance of the binary_writer class for the specified stream.

Parameters
streamThe stream to be read.

Member Function Documentation

◆ base_stream()

std::optional<std::reference_wrapper<std::ostream> > xtd::io::binary_writer::base_stream ( ) const

Returns the underlying stream.

Returns
The underlying stream.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ close()

virtual void xtd::io::binary_writer::close ( )
virtual

Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources associated with the reader.

◆ flush()

virtual void xtd::io::binary_writer::flush ( )
virtual

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

Remarks
This default method does nothing, but derived classes can virtual the method to provide the appropriate functionality

◆ seek()

virtual size_t xtd::io::binary_writer::seek ( size_t  offset,
std::ios::seekdir  origin 
)
virtual

Sets the position within the current stream.

Parameters
ofssetA byte offset relative to origin.
originA field of std::ios::seekdir indicating the reference point from which the new position is to be obtained.
Returns
The position with the current stream.
Exceptions
xtd::io::io_exceptionThe file pointer was moved to an invalid location.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [1/19]

virtual void xtd::io::binary_writer::write ( bool  value)
virtual

Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing true.

Parameters
valueThe boolean value to write (0 or 1).
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [2/19]

virtual void xtd::io::binary_writer::write ( byte_t  value)
virtual

Writes an unsigned byte to the current stream and advances the stream position by one byte.

Parameters
valueThe unsigned byte to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [3/19]

virtual void xtd::io::binary_writer::write ( char  value)
virtual

Writes a character to the current stream and advances the current position by one byte.

Parameters
valueThe character to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [4/19]

template<size_t size>
void xtd::io::binary_writer::write ( const std::array< byte_t, size > &  buffer)
inline

Writes a byte array to the underlying stream.

Parameters
bufferA byte array containing the data to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [5/19]

template<size_t size>
void xtd::io::binary_writer::write ( const std::array< char, size > &  buffer)
inline

Writes a character array to the underlying stream.

Parameters
bufferA character array containing the data to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [6/19]

virtual void xtd::io::binary_writer::write ( const std::vector< byte_t > &  buffer)
virtual

Writes a byte array to the underlying stream.

Parameters
bufferA byte array containing the data to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [7/19]

virtual void xtd::io::binary_writer::write ( const std::vector< byte_t > &  buffer,
size_t  index,
size_t  count 
)
virtual

Writes a region of a byte array to the current stream.

Parameters
bufferA byte array containing the data to write.
indexThe index of the first byte to read from buffer and to write to the stream.
countThe number of bytes to read from buffer and to write to the stream.
Exceptions
xtd::argument_exceptionThe buffer length minus index is less than count.
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [8/19]

virtual void xtd::io::binary_writer::write ( const std::vector< char > &  buffer)
virtual

Writes a character array to the underlying stream.

Parameters
bufferA character array containing the data to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [9/19]

virtual void xtd::io::binary_writer::write ( const std::vector< char > &  buffer,
size_t  index,
size_t  count 
)
virtual

Writes a region of a character array to the current stream.

Parameters
bufferA character array containing the data to write.
indexThe index of the first byte to read from buffer and to write to the stream.
countThe number of bytes to read from buffer and to write to the stream.
Exceptions
xtd::argument_exceptionThe buffer length minus index is less than count.
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [10/19]

virtual void xtd::io::binary_writer::write ( double  value)
virtual

Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

Parameters
valueThe eight-byte floating-point value to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [11/19]

virtual void xtd::io::binary_writer::write ( int16_t  value)
virtual

Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.

Parameters
valueThe two-byte signed integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [12/19]

virtual void xtd::io::binary_writer::write ( int32_t  value)
virtual

Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

Parameters
valueThe four-byte signed integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [13/19]

virtual void xtd::io::binary_writer::write ( int64_t  value)
virtual

Writes a eight-byte signed integer to the current stream and advances the stream position by eight bytes.

Parameters
valueThe eight-byte signed integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [14/19]

virtual void xtd::io::binary_writer::write ( sbyte_t  value)
virtual

Writes an signed byte to the current stream and advances the stream position by one byte.

Parameters
valueThe signed byte to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [15/19]

virtual void xtd::io::binary_writer::write ( float  value)
virtual

Writes an four-byte floating-point value to the current stream and advances the stream position by four bytes.

Parameters
valueThe four-byte floating-point value to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [16/19]

virtual void xtd::io::binary_writer::write ( const ustring value)
virtual

Writes a length-prefixed string to this stream, and advances the current position of the stream and the specific characters being written to the stream.

Parameters
valueThe value to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [17/19]

virtual void xtd::io::binary_writer::write ( uint16_t  value)
virtual

Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

Parameters
valueThe two-byte unsigned integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [18/19]

virtual void xtd::io::binary_writer::write ( uint32_t  value)
virtual

Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.

Parameters
valueThe four-byte unsigned integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ write() [19/19]

virtual void xtd::io::binary_writer::write ( uint64_t  value)
virtual

Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.

Parameters
valueThe eight-byte unsigned integer to write.
Exceptions
xtd::io::io_exceptionAn I/O error occurs.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

The documentation for this class was generated from the following file: