}
void
-write_file(const std::string& path, const std::string& data, bool binary)
+write_file(const std::string& path,
+ const std::string& data,
+ std::ios_base::openmode open_mode)
{
- std::ofstream file(path,
- binary ? std::ios::out | std::ios::binary : std::ios::out);
+ open_mode |= std::ios::out;
+ std::ofstream file(path, open_mode);
if (!file) {
throw Error(strerror(errno));
}
// Throws Error on error.
void wipe_path(const std::string& path);
-// Write file data from a string.
+// Write file data from a string. The file will be opened according to
+// `open_mode`, which always will include `std::ios::out` even if not specified
+// at the call site.
//
// Throws `Error` on error. The description contains the error message without
// the path.
void write_file(const std::string& path,
const std::string& data,
- bool binary = false);
+ std::ios_base::openmode open_mode = std::ios::out);
} // namespace Util
std::string data = Util::read_file("test");
CHECK(data == "foo\nbar\n");
+ Util::write_file("test", "car");
+ data = Util::read_file("test");
+ CHECK(data == "car");
+
+ Util::write_file("test", "pet", std::ios::app);
+ data = Util::read_file("test");
+ CHECK(data == "carpet");
+
CHECK_THROWS_WITH(Util::read_file("does/not/exist"),
Equals("No such file or directory"));