add new trunc option to write_file
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 20 Aug 2013 13:55:48 +0000 (15:55 +0200)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 20 Aug 2013 13:55:48 +0000 (15:55 +0200)
src/filefunc.cpp
src/filefunc.hxx

index 0836d37..c427634 100644 (file)
@@ -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() );
index d901e8d..8ea0e1b 100644 (file)
@@ -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);