xtd 0.2.0
file_info_open_write.cpp

Show how to use xtd::io::file_info::open_write method.

#include <xtd/io/file_info>
#include <xtd/io/stream_reader>
#include <xtd/io/stream_writer>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
auto path = "MyTest.txt";
auto fi = file_info {path};
// Delete the file if it exists.
if (fi.exists())
fi.remove();
// Open the stream for writing.
block_scope_(auto fs = fi.open_write()) {
auto sw = stream_writer {fs};
sw.write_line("This is some text in the file.");
}
//Open the stream and read it back.
block_scope_(auto fs = fi.open_read()) {
auto sr = stream_reader {fs};
while (!sr.end_of_stream())
console::write_line(sr.read_line());
}
}
};
startup_(program::main);
// This code produces the following output :
//
// This is some text in the file.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.hpp:41
void remove() const override
Permanently deletes a file.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.hpp:28
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.hpp:28
void write_line()
Writes new line to the text stream.
#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:167
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.hpp:25
@ path
The xtd::uri::local_path data.
Definition uri_components.hpp:27
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8