xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
file.h
Go to the documentation of this file.
1 #pragma once
5 
6 #include <cstdint>
7 #include <cstdio>
8 #include <fstream>
9 #include <vector>
10 #include "../core_export.h"
11 #include "../static.h"
12 #include "../ustring.h"
13 #include "file_attributes.h"
14 #include "stream_reader.h"
15 #include "stream_writer.h"
16 
18 namespace xtd {
20  namespace io {
28  public:
36  template<typename type_t>
37  static void append_all_lines(const xtd::ustring& path, type_t contents) {
38  xtd::io::stream_writer sw(path, true);
39  for (const auto& line : contents)
40  sw.write_line(line);
41  }
42 
50  template<typename type_t>
51  static void append_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
52  xtd::io::stream_writer sw(path, true);
53  for (const auto& line : contents)
54  sw.write_line(line);
55  }
56 
63  template<typename type_t>
64  static void append_all_text(const xtd::ustring& path, type_t text) {
65  xtd::io::stream_writer sw(path, true);
66  sw.write(text);
67  }
68 
74  static std::ofstream append_text(const xtd::ustring& path);
75 
82  static void copy(const xtd::ustring& src, const xtd::ustring& dest);
83 
90  static void copy(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
91 
97  static std::ofstream create(const xtd::ustring& path);
98 
105  static std::ofstream create_text(const xtd::ustring& path);
106 
112  static bool exists(const xtd::ustring& path) noexcept;
113 
119 
127  static void move(const xtd::ustring& src, const xtd::ustring& dest);
128 
135  static std::fstream open(const xtd::ustring& path, std::ios::openmode mode);
136 
142  static std::ifstream open_read(const xtd::ustring& path);
143 
149  static std::ifstream open_text(const xtd::ustring& path);
150 
156  static std::ofstream open_write(const xtd::ustring& path);
157 
164  static std::vector<uint8_t> read_all_bytes(const xtd::ustring& path);
165 
172  template<typename char_t>
173  static std::vector<uint8_t> read_all_bytes(const char_t* path) {return read_all_bytes(xtd::ustring(path));}
174 
180  static std::vector<xtd::ustring> read_all_lines(const xtd::ustring& path);
181 
188 
193  static void remove(const xtd::ustring& path);
194 
201  static void replace(const xtd::ustring& source_file_name, const xtd::ustring& destination_file_name, const xtd::ustring& destination_backup_file_name);
202 
210  template<typename type_t>
211  static void write_all_lines(const xtd::ustring& path, type_t contents) {
213  for (const auto& line : contents)
214  sw.write_line(line);
215  }
216 
224  template<typename type_t>
225  static void write_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
227  for (const auto& line : contents)
228  sw.write_line(line);
229  }
230 
237  template<typename type_t>
238  static void write_all_text(const xtd::ustring& path, type_t text) {
240  sw.write(text);
241  }
242 
248  static std::ofstream write_text(const xtd::ustring& path);
249  };
250  }
251 }
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition: file.h:27
static std::ifstream open_read(const xtd::ustring &path)
Opens an existing file for reading.
static void move(const xtd::ustring &src, const xtd::ustring &dest)
Moves a specified file to a new location, providing the option to specify a new file name.
static void replace(const xtd::ustring &source_file_name, const xtd::ustring &destination_file_name, const xtd::ustring &destination_backup_file_name)
Replaces the contents of a specified file with the contents of another file, deleting the original fi...
static void append_all_text(const xtd::ustring &path, type_t text)
Appends text to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:64
static void write_all_lines(const xtd::ustring &path, type_t contents)
Writes lines to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:211
static std::vector< uint8_t > read_all_bytes(const char_t *path)
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
Definition: file.h:173
static std::ofstream write_text(const xtd::ustring &path)
Creates a std::ofstream that appends text to an existing file.
static std::vector< uint8_t > read_all_bytes(const xtd::ustring &path)
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
static void remove(const xtd::ustring &path)
Deletes the specified file.
static std::fstream open(const xtd::ustring &path, std::ios::openmode mode)
Opens a FileStream on the specified path.
static void copy(const xtd::ustring &src, const xtd::ustring &dest, bool overwrite)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
static std::ofstream create_text(const xtd::ustring &path)
Creates or opens a file for writing text.
static void append_all_lines(const xtd::ustring &path, const std::initializer_list< type_t > &contents)
Appends lines to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:51
static std::ofstream open_write(const xtd::ustring &path)
Opens an existing file for writing.
static void write_all_lines(const xtd::ustring &path, const std::initializer_list< type_t > &contents)
Writes lines to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:225
static std::ifstream open_text(const xtd::ustring &path)
Opens an existing file for reading.
static void append_all_lines(const xtd::ustring &path, type_t contents)
Appends lines to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:37
static void write_all_text(const xtd::ustring &path, type_t text)
Writes text to a file, and then closes the file. If the specified file does not exist,...
Definition: file.h:238
static void copy(const xtd::ustring &src, const xtd::ustring &dest)
Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
static std::vector< xtd::ustring > read_all_lines(const xtd::ustring &path)
Opens a text file, reads all lines of the file, and then closes the file.
static xtd::io::file_attributes get_attributes(const xtd::ustring &src)
Gets the xtd::io::file_attributes of the file on the path.
static xtd::ustring read_all_text(const xtd::ustring &path)
Opens a text file, reads all text of the file, and then closes the file.
static std::ofstream create(const xtd::ustring &path)
Creates or overwrites a file in the specified path.
static bool exists(const xtd::ustring &path) noexcept
Determines whether the specified file exists.
static std::ofstream append_text(const xtd::ustring &path)
Creates a std::ofstream that appends text to an existing file.
Performs operations on std::basic_string instances that contain file or directory path information....
Definition: path.h:29
Implements a xtd::io::text_writer for writing characters to a stream.
Definition: stream_writer.h:20
void write(const xtd::ustring &value) override
Writes the specified string value to the text stream.
void write_line()
Writes new line to the text stream.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Contains xtd::io::file_attributes enum class.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition: static.h:38
#define core_export_
Define shared library export.
Definition: core_export.h:13
file_attributes
Provides attributes for files and directories.
Definition: file_attributes.h:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::io::stream_reader class.
Contains xtd::io::stream_writer class.