Reads primitive data types as binary values in a specific encoding.
binary_reader(const xtd::string &path)
Initializes a new instance of the xtd::io::binary_reader 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_reader
- Header
#include <xtd/io/binary_reader>
- 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.
|
| auto | close () -> void |
| | Closes the xtd::io::binary_reader object and the underlying stream, and releases any system resources associated with the reader.
|
| |
| auto | peek_char () const -> xtd::int32 |
| | Returns the next available character and does not advance the byte or character position.
|
| |
| auto | pop () -> std::streampos |
| | Pop the current top position.
|
| |
| auto | push (std::streampos pos=0) -> void |
| | Push the current position.
|
| |
| virtual auto | read () -> xtd::int32 |
| | Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
|
| |
| virtual auto | read (xtd::array< xtd::byte > &buffer, xtd::size index, xtd::size count) -> xtd::size |
| | Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
|
| |
| virtual auto | read (xtd::array< char > &buffer, xtd::size index, xtd::size count) -> xtd::size |
| | Reads the specified number of characters from the stream, starting from a specified point in the character array.
|
| |
| virtual auto | read_boolean () -> bool |
| | Reads a boolean value from the current stream and advances the current position of the stream by one byte.
|
| |
| virtual auto | read_byte () -> xtd::byte |
| | Reads the next byte from the current stream and advances the current position of the stream by one byte.
|
| |
| virtual auto | read_bytes (xtd::size count) -> xtd::array< xtd::byte > |
| | Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
|
| |
| virtual auto | read_char () -> char |
| | Reads the next character from the current stream and advances the current position of the stream by one byte.
|
| |
| virtual auto | read_chars (xtd::size count) -> xtd::array< char > |
| | Reads the specified number of characters from the current stream into a byte array and advances the current position by that number of bytes.
|
| |
| virtual auto | read_double () -> double |
| | Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
| |
| virtual auto | read_int16 () -> xtd::int16 |
| | Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
| |
| virtual auto | read_int32 () -> xtd::int32 |
| | Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
| |
| virtual auto | read_int64 () -> xtd::int64 |
| | Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
| |
| virtual auto | read_sbyte () -> xtd::sbyte |
| | Reads the a signed byte from the current stream and advances the current position of the stream by one byte.
|
| |
| virtual auto | read_single () -> float |
| | Reads an 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
| |
| virtual auto | read_size () -> xtd::size |
| | Reads a xtd::size from the current stream and advances the current position of the stream by eight bytes.
|
| |
| virtual auto | read_string () -> xtd::string |
| | Reads a string from the current stream. The string is prefixed with the length.
|
| |
| virtual auto | read_uint16 () -> xtd::uint16 |
| | Reads a 2-byte unsigned integer from the current stream and advances the current position of the stream by two bytes.
|
| |
| virtual auto | read_uint32 () -> xtd::uint32 |
| | Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
|
| |
| virtual auto | read_uint64 () -> xtd::uint64 |
| | Reads a 8-byte unsigned integer from the current stream and advances the current position of the stream by eight bytes.
|
| |
| auto | rewind () -> void |
| | Rewind stream.
|
| |
| virtual auto | seek (std::streamoff off, std::ios_base::seekdir dir=std::ios_base::cur) -> void |
| | Change the position of the readers stream.
|
| |
| auto | tell () -> std::streampos |
| | Tell the current seek position of the readers stream;.
|
| |
|
| | 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.
|
| |