xtd 0.2.0
Loading...
Searching...
No Matches
xtd::io::binary_writer Class Reference
Inheritance diagram for xtd::io::binary_writer:
xtd::object

Definition

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

binary_writer(const xtd::string &path)
Initializes a new instance of the binary_writer class for the specified file name.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
#define core_export_
Define shared library export.
Definition core_export.hpp:13
Inheritance
xtd::objectxtd::io::binary_writer
Header
#include <xtd/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>
class program {
inline static const string file_name = "app_settings.dat";
public:
static auto main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(auto fs = file::open(file_name, std::ios::out | std::ios::binary | std::ios::trunc)) {
auto writer = binary_writer {fs};
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
auto aspect_ratio = .0f;
auto temp_directory = string::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
using_(auto fs = file::open(file_name, std::ios::in | std::ios::binary)) {
auto reader = binary_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::main);
// 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
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.hpp:47
virtual auto write(bool value) -> void
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
static auto open(const xtd::string &path, std::ios::openmode mode) -> std::fstream
Opens a FileStream on the specified path.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:282
#define using_(...)
The specified expression is cleared automatically when the scope is ended.
Definition using.hpp:33
Examples
The same example with stream operators.
#include <xtd/xtd>
class program {
inline static const string file_name = "app_settings.dat";
public:
static auto main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(auto fs = file::open(file_name, std::ios::out | std::ios::binary | std::ios::trunc)) {
auto writer = binary_writer {fs};
writer << 1.250F;
writer << R"(c:\Temp)";
writer << 10;
writer << true;
}
}
static void display_values() {
auto aspect_ratio = .0f;
auto temp_directory = string::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
using_(auto fs = file::open(file_name, std::ios::in | std::ios::binary)) {
auto reader = binary_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::main);
// 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.
Note
xtd xtd::io::binary_reader and xtd::io::binary_writer are fully compatible with .NET BinaryReader/BinaryWriter.
Files written with xtd can be read directly in C# and vice versa, following the same binary format conventions (7-bit encoded string lengths, little-endian integers, etc.).
Examples
binary_reader3.cpp.

Public Constructors

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

Public Properties

auto base_stream () const -> std::optional< xtd::ref< std::ostream > >
 Returns the underlying stream.
 

Public Methods

virtual auto close () -> void
 Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources associated with the reader.
 
virtual auto flush () -> void
 Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
 
virtual auto seek (xtd::size offset, std::ios::seekdir origin) -> xtd::size
 Sets the position within the current stream.
 
virtual auto tell () -> std::streampos
 Tell the current seek position of the readers stream;.
 
virtual auto write (bool value) -> void
 Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing true.
 
virtual auto write (xtd::byte value) -> void
 Writes an unsigned byte to the current stream and advances the stream position by one byte.
 
virtual auto write (char value) -> void
 Writes a character to the current stream and advances the current position by one byte.
 
template<xtd::size size>
auto write (const std::array< xtd::byte, size > &buffer) -> void
 Writes a byte array to the underlying stream.
 
template<xtd::size size>
auto write (const std::array< char, size > &buffer) -> void
 Writes a character array to the underlying stream.
 
virtual auto write (const xtd::read_only_span< xtd::byte > &buffer) -> void
 Writes a byte array to the underlying stream.
 
virtual auto write (const xtd::array< xtd::byte > &buffer, xtd::size index, xtd::size count) -> void
 Writes a region of a byte array to the current stream.
 
virtual auto write (const xtd::read_only_span< char > &buffer) -> void
 Writes a character array to the underlying stream.
 
virtual auto write (const xtd::array< char > &buffer, xtd::size index, xtd::size count) -> void
 Writes a region of a character array to the current stream.
 
virtual auto write (double value) -> void
 Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.
 
virtual auto write (int16 value) -> void
 Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
 
virtual auto write (xtd::int32 value) -> void
 Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.
 
virtual auto write (xtd::int64 value) -> void
 Writes a eight-byte signed integer to the current stream and advances the stream position by eight bytes.
 
virtual auto write (xtd::sbyte value) -> void
 Writes an signed byte to the current stream and advances the stream position by one byte.
 
virtual auto write (float value) -> void
 Writes an four-byte floating-point value to the current stream and advances the stream position by four bytes.
 
virtual auto write (const xtd::string &value) -> void
 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.
 
virtual auto write (xtd::uint16 value) -> void
 Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.
 
virtual auto write (xtd::uint32 value) -> void
 Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.
 
virtual auto write (xtd::uint64 value) -> void
 Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.
 

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual xtd::size get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<class object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const
 Returns a xtd::string that represents the current object.
 
template<class object_a_t, class object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<class object_a_t, class object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

Constructor & Destructor Documentation

◆ binary_writer() [1/2]

xtd::io::binary_writer::binary_writer ( const xtd::string & 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()

auto xtd::io::binary_writer::base_stream ( ) const -> std::optional< xtd::ref< std::ostream > >
nodiscard

Returns the underlying stream.

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

◆ close()

virtual auto xtd::io::binary_writer::close ( ) -> void
virtual

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

◆ flush()

virtual auto xtd::io::binary_writer::flush ( ) -> void
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 auto xtd::io::binary_writer::seek ( xtd::size offset,
std::ios::seekdir origin ) -> xtd::size
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.

◆ tell()

virtual auto xtd::io::binary_writer::tell ( ) -> std::streampos
nodiscardvirtual

Tell the current seek position of the readers stream;.

Returns
The current seek position

◆ write() [1/19]

virtual auto xtd::io::binary_writer::write ( bool value) -> void
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 auto xtd::io::binary_writer::write ( xtd::byte value) -> void
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 auto xtd::io::binary_writer::write ( char value) -> void
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<xtd::size size>
auto xtd::io::binary_writer::write ( const std::array< xtd::byte, size > & buffer) -> void
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<xtd::size size>
auto xtd::io::binary_writer::write ( const std::array< char, size > & buffer) -> void
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 auto xtd::io::binary_writer::write ( const xtd::read_only_span< xtd::byte > & buffer) -> void
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 auto xtd::io::binary_writer::write ( const xtd::array< xtd::byte > & buffer,
xtd::size index,
xtd::size count ) -> void
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 auto xtd::io::binary_writer::write ( const xtd::read_only_span< char > & buffer) -> void
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 auto xtd::io::binary_writer::write ( const xtd::array< char > & buffer,
xtd::size index,
xtd::size count ) -> void
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 auto xtd::io::binary_writer::write ( double value) -> void
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 auto xtd::io::binary_writer::write ( int16 value) -> void
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 auto xtd::io::binary_writer::write ( xtd::int32 value) -> void
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 auto xtd::io::binary_writer::write ( xtd::int64 value) -> void
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 auto xtd::io::binary_writer::write ( xtd::sbyte value) -> void
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 auto xtd::io::binary_writer::write ( float value) -> void
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 auto xtd::io::binary_writer::write ( const xtd::string & value) -> void
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 auto xtd::io::binary_writer::write ( xtd::uint16 value) -> void
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 auto xtd::io::binary_writer::write ( xtd::uint32 value) -> void
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 auto xtd::io::binary_writer::write ( xtd::uint64 value) -> void
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: