xtd 0.2.0
Loading...
Searching...
No Matches

◆ append_text()

xtd::io::stream_writer xtd::io::file_info::append_text ( ) const

Creates a xtd::io::stream_writer that appends text to the file represented by this instance of the xtd::io::file_info.

Returns
a new A new std::ofstream.
Examplle
The following example appends text to a file and reads from the file.
#include <xtd/io/file_info>
#include <xtd/io/path>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
auto fi = file_info {path::combine(path::get_temp_path(), "MyTest.txt")};
// This text is added only once to the file.
if (!fi.exists()) {
//Create a file to write to.
block_scope_(auto sw = fi.create_text()) {
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
block_scope_(auto sw = fi.append_text()) {
sw.write_line("This");
sw.write_line("is Extra");
sw.write_line("Text");
}
//Open the file to read from.
block_scope_(auto sr = fi.open_text()) {
while (!sr.end_of_stream())
console::write_line(sr.read_line());
}
}
};
startup_(program::main);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// Hello
// And
// Welcome
// This
// is Extra
// Text
//When you run this application a second time, you will see the following output :
//
// Hello
// And
// Welcome
// This
// is Extra
// Text
// This
// is Extra
// Text
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
#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 following example demonstrates appending text to the end of a file and also displays the result of the append operation to the console. The first time this routine is called, the file is created if it does not exist. After that, the specified text is appended to the file.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
static auto main() {
file_info fi("temp.txt");
// Create a writer, ready to add entries to the file.
stream_writer sw = fi.append_text();
sw.write_line("Add as many lines as you like...");
sw.write_line("Add another line to the output...");
sw.flush();
sw.close();
// Get the information out of the file and display it.
// Remember that the file might have other lines if it already existed.
std::ifstream ifs(fi.open_read());
stream_reader sr(ifs);
while (sr.peek() != -1)
console::write_line(sr.read_line());
}
};
startup_(program::main);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
// Add as many lines as you like...
// Add another line to the output...
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
void flush() override
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
void close() override
Closes the stream_writer object and the underlying stream, and releases any system resources associat...
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:28
void write_line()
Writes new line to the text stream.