xtd 0.2.0
Loading...
Searching...
No Matches
binary_reader.cpp

Show how to use xtd::io::binary_reader class.

#include <xtd/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/console>
#include <xtd/startup>
#include <xtd/using>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring 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, ios::out | ios::binary | 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 = ustring::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
using_(auto fs = file::open(file_name, ios::in | 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:38
virtual float read_single()
Reads an 4-byte floating point value from the current stream and advances the current position of the...
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.h:37
virtual void write(bool value)
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
#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:166
#define using_(...)
The specified expression is cleared automatically when the scope is ended.
Definition using.h:33
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