* @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() );
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);