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

◆ create_text()

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

Creates a std::ofstream that writes a new text file.

Returns
A new std::ofstream.
Exceptions
xtd::unauthorized_access_exceptionThe file name is a directory.
xtd::io::io_exceptionThe disk is read-only.
xtd::security::security_exceptionThe caller does not have the required permission.
Examples
The following example demonstrates the xtd::io::file_info::create_text method.
#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 path = path::combine(path::get_temp_path(), "MyTest.txt");
auto fi = file_info {path};
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");
}
}
//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
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:36
#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
Remarks
By default, full read/write access to new files is granted to all users.
Examples
file_info.cpp.