add new trunc option to write_file
[libi2ncommon] / src / filefunc.cpp
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() );