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

◆ create()

std::ofstream xtd::io::file_info::create ( ) const

Creates a file.

Returns
A new file.
Examples
The following example creates a reference to a file, and then creates the file on disk using xtd::io::file_info.create().
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
// Create a reference to a file.
file_info fi("temp.txt");
// Actually create the file.
std::ofstream ofs( fi.create());
// Modify the file as required, and then close the file.
ofs.close();
// Delete the file.
fi.remove();
}
};
startup_(program::main);
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
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 creates a file, adds some text to it, and reads from the file.
#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();
//Create the file.
block_scope_(auto fs = fi.create()) {
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(std::ios::in)) {
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.
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:36
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
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.
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
Remarks
By default, full read/write access to new files is granted to all users.
This method is a wrapper for the functionality provided by xtd::io::file::create.