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::object → xtd::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)) {
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();
}
}
}
};
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)) {
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;
}
}
}
};
- 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.
|
| 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.
|
| |
|
| | 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.
|
| |