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.

Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.h:42
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define core_export_
Define shared library export.
Definition core_export.h: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/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
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() {
block_scope_(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)) {
block_scope_(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
Reads primitive data types as binary values in a specific encoding.
Definition binary_reader.h:40
virtual float read_single()
Reads an 4-byte floating point value from the current stream and advances the current position of the...
virtual void write(bool value)
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
The same example with stream operators.
#include <xtd/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
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() {
block_scope_(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)) {
block_scope_(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.
Examples
binary_reader.cpp, binary_reader2.cpp, and 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

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

Public Methods

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

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 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 size_t 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<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename 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<typename object_a_t , typename 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()

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.
Examples
binary_reader.cpp.

◆ write() [2/19]

virtual void xtd::io::binary_writer::write ( xtd::byte  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< xtd::byte, 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< xtd::byte > &  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< xtd::byte > &  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  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  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  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  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 string 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  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  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  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: