From 71f7bfb868201b018f5aaa8e63b3401a1b0243d9 Mon Sep 17 00:00:00 2001 From: Gerd von Egidy Date: Tue, 20 Aug 2013 15:55:48 +0200 Subject: [PATCH] add new trunc option to write_file --- src/filefunc.cpp | 10 ++++++++-- src/filefunc.hxx | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/filefunc.cpp b/src/filefunc.cpp index 0836d37..c427634 100644 --- a/src/filefunc.cpp +++ b/src/filefunc.cpp @@ -440,13 +440,19 @@ std::string read_file(const std::string& path) * @brief writes a string to a file. * @param path path to the file * @param data the data which should be written into the file + * @param trunc set the trunc flag when opening the file. Do not use for files in /proc and /sys * @return @a true if the data was written to the file. * * A simple (q'n'd) function for writing a string to a file. */ -bool write_file(const std::string& path, const std::string& data) +bool write_file(const std::string& path, const std::string& data, bool trunc) { - std::ofstream f( path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); + // set the correct openmode flags + std::ios_base::openmode flags = std::ios::out | std::ios::binary; + if (trunc) + flags |= std::ios::trunc; + + std::ofstream f( path.c_str(), flags); if (f.good()) { f.write( data.data(), data.size() ); diff --git a/src/filefunc.hxx b/src/filefunc.hxx index d901e8d..8ea0e1b 100644 --- a/src/filefunc.hxx +++ b/src/filefunc.hxx @@ -159,7 +159,7 @@ std::string read_link(const std::string& path); std::string read_file(const std::string& path); -bool write_file(const std::string& path, const std::string& data); +bool write_file(const std::string& path, const std::string& data, bool trunc=true); bool copy_file(const std::string& src, const std::string& dest); bool copy_stream(std::istream& is, std::ostream& os); -- 1.7.1