76 std::wstring wide_path;
77 try { wide_path = string_tools::utf8_to_utf16(path_to_file); }
catch (...) {
return false; }
78 HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
79 if (file_handle == INVALID_HANDLE_VALUE)
82 DWORD bytes_to_write = (DWORD)str.size();
83 BOOL result = WriteFile(file_handle, str.data(), bytes_to_write, &bytes_written, NULL);
84 CloseHandle(file_handle);
85 if (bytes_written != bytes_to_write)
91 std::ofstream fstream;
92 fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
93 fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
130 bool load_file_to_string(
const std::string& path_to_file, std::string& target_str,
size_t max_size = 1000000000)
133 std::wstring wide_path;
134 try { wide_path = string_tools::utf8_to_utf16(path_to_file); }
catch (...) {
return false; }
135 HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
136 if (file_handle == INVALID_HANDLE_VALUE)
138 DWORD file_size = GetFileSize(file_handle, NULL);
139 if ((file_size == INVALID_FILE_SIZE) || (
uint64_t)file_size > (
uint64_t)max_size) {
140 CloseHandle(file_handle);
143 target_str.resize(file_size);
145 BOOL result = ReadFile(file_handle, &target_str[0], file_size, &bytes_read, NULL);
146 CloseHandle(file_handle);
147 if (bytes_read != file_size)
153 std::ifstream fstream;
154 fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
155 fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
157 std::ifstream::pos_type file_size = fstream.tellg();
161 size_t file_size_t =
static_cast<size_t>(file_size);
163 target_str.resize(file_size_t);
165 fstream.seekg (0, std::ios::beg);
166 fstream.read((
char*)target_str.data(), target_str.size());
202 std::wstring wide_path;
203 try { wide_path = string_tools::utf8_to_utf16(path_to_file); }
catch (...) {
return false; }
204 HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
205 if (file_handle == INVALID_HANDLE_VALUE)
207 LARGE_INTEGER file_size;
208 BOOL result = GetFileSizeEx(file_handle, &file_size);
209 CloseHandle(file_handle);
211 size = file_size.QuadPart;
217 std::ifstream fstream;
218 fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
219 fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate);
220 size = fstream.tellg();