<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Config Datei lesen&#x2F;schreiben]]></title><description><![CDATA[<p>Hallo!</p>
<p>Habe auf <a href="http://www-personal.umich.edu/~wagnerr/ConfigFile.html" rel="nofollow">http://www-personal.umich.edu/~wagnerr/ConfigFile.html</a> ein Klasse gefunden die Konfigurationsdateien ließt, verändern und schreiben soll.</p>
<p>ConfigFile.cpp:</p>
<pre><code class="language-cpp">#include &quot;ConfigFile.h&quot;

using std::string;

ConfigFile::ConfigFile( string filename, string delimiter,
                        string comment, string sentry )
	: myDelimiter(delimiter), myComment(comment), mySentry(sentry)
{
	// Construct a ConfigFile, getting keys and values from given file

	std::ifstream in( filename.c_str() );

	if( !in ) throw file_not_found( filename ); 

	in &gt;&gt; (*this);
}

ConfigFile::ConfigFile()
	: myDelimiter( string(1,'=') ), myComment( string(1,'#') )
{
	// Construct a ConfigFile without a file; empty
}

void ConfigFile::remove( const string&amp; key )
{
	// Remove key and its value
	myContents.erase( myContents.find( key ) );
	return;
}

bool ConfigFile::keyExists( const string&amp; key ) const
{
	// Indicate whether key is found
	mapci p = myContents.find( key );
	return ( p != myContents.end() );
}

/* static */
void ConfigFile::trim( string&amp; s )
{
	// Remove leading and trailing whitespace
	static const char whitespace[] = &quot; \n\t\v\r\f&quot;;
	s.erase( 0, s.find_first_not_of(whitespace) );
	s.erase( s.find_last_not_of(whitespace) + 1U );
}

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const ConfigFile&amp; cf )
{
	// Save a ConfigFile to os
	for( ConfigFile::mapci p = cf.myContents.begin();
	     p != cf.myContents.end();
		 ++p )
	{
		os &lt;&lt; p-&gt;first &lt;&lt; &quot; &quot; &lt;&lt; cf.myDelimiter &lt;&lt; &quot; &quot;;
		os &lt;&lt; p-&gt;second &lt;&lt; std::endl;
	}
	return os;
}

std::istream&amp; operator&gt;&gt;( std::istream&amp; is, ConfigFile&amp; cf )
{
	// Load a ConfigFile from is
	// Read in keys and values, keeping internal whitespace
	typedef string::size_type pos;
	const string&amp; delim  = cf.myDelimiter;  // separator
	const string&amp; comm   = cf.myComment;    // comment
	const string&amp; sentry = cf.mySentry;     // end of file sentry
	const pos skip = delim.length();        // length of separator

	string nextline = &quot;&quot;;  // might need to read ahead to see where value ends

	while( is || nextline.length() &gt; 0 )
	{
		// Read an entire line at a time
		string line;
		if( nextline.length() &gt; 0 )
		{
			line = nextline;  // we read ahead; use it now
			nextline = &quot;&quot;;
		}
		else
		{
			std::getline( is, line );
		}

		// Ignore comments
		line = line.substr( 0, line.find(comm) );

		// Check for end of file sentry
		if( sentry != &quot;&quot; &amp;&amp; line.find(sentry) != string::npos ) return is;

		// Parse the line if it contains a delimiter
		pos delimPos = line.find( delim );
		if( delimPos &lt; string::npos )
		{
			// Extract the key
			string key = line.substr( 0, delimPos );
			line.replace( 0, delimPos+skip, &quot;&quot; );

			// See if value continues on the next line
			// Stop at blank line, next line with a key, end of stream,
			// or end of file sentry
			bool terminate = false;
			while( !terminate &amp;&amp; is )
			{
				std::getline( is, nextline );
				terminate = true;

				string nlcopy = nextline;
				ConfigFile::trim(nlcopy);
				if( nlcopy == &quot;&quot; ) continue;

				nextline = nextline.substr( 0, nextline.find(comm) );
				if( nextline.find(delim) != string::npos )
					continue;
				if( sentry != &quot;&quot; &amp;&amp; nextline.find(sentry) != string::npos )
					continue;

				nlcopy = nextline;
				ConfigFile::trim(nlcopy);
				if( nlcopy != &quot;&quot; ) line += &quot;\n&quot;;
				line += nextline;
				terminate = false;
			}

			// Store key and value
			ConfigFile::trim(key);
			ConfigFile::trim(line);
			cf.myContents[key] = line;  // overwrites if key is repeated
		}
	}

	return is;
}
</code></pre>
<p>ConfigFile.h</p>
<pre><code class="language-cpp">// Typical usage
// -------------
// 
// Given a configuration file &quot;settings.inp&quot;:
//   atoms  = 25
//   length = 8.0  # nanometers
//   name = Reece Surcher
// 
// Named values are read in various ways, with or without default values:
//   ConfigFile config( &quot;settings.inp&quot; );
//   int atoms = config.read&lt;int&gt;( &quot;atoms&quot; );
//   double length = config.read( &quot;length&quot;, 10.0 );
//   string author, title;
//   config.readInto( author, &quot;name&quot; );
//   config.readInto( title, &quot;title&quot;, string(&quot;Untitled&quot;) );
// 
// See file example.cpp for more examples.

#ifndef CONFIGFILE_H
#define CONFIGFILE_H

#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;

using std::string;

class ConfigFile {
// Data
protected:
	string myDelimiter;  // separator between key and value
	string myComment;    // separator between value and comments
	string mySentry;     // optional string to signal end of file
	std::map&lt;string,string&gt; myContents;  // extracted keys and values

	typedef std::map&lt;string,string&gt;::iterator mapi;
	typedef std::map&lt;string,string&gt;::const_iterator mapci;

// Methods
public:
	ConfigFile( string filename,
	            string delimiter = &quot;=&quot;,
	            string comment = &quot;#&quot;,
				string sentry = &quot;EndConfigFile&quot; );
	ConfigFile();

	// Search for key and read value or optional default value
	template&lt;class T&gt; T read( const string&amp; key ) const;  // call as read&lt;T&gt;
	template&lt;class T&gt; T read( const string&amp; key, const T&amp; value ) const;
	template&lt;class T&gt; bool readInto( T&amp; var, const string&amp; key ) const;
	template&lt;class T&gt;
	bool readInto( T&amp; var, const string&amp; key, const T&amp; value ) const;

	// Modify keys and values
	template&lt;class T&gt; void add( string key, const T&amp; value );
	void remove( const string&amp; key );

	// Check whether key exists in configuration
	bool keyExists( const string&amp; key ) const;

	// Check or change configuration syntax
	string getDelimiter() const { return myDelimiter; }
	string getComment() const { return myComment; }
	string getSentry() const { return mySentry; }
	string setDelimiter( const string&amp; s )
		{ string old = myDelimiter;  myDelimiter = s;  return old; }  
	string setComment( const string&amp; s )
		{ string old = myComment;  myComment = s;  return old; }

	// Write or read configuration
	friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const ConfigFile&amp; cf );
	friend std::istream&amp; operator&gt;&gt;( std::istream&amp; is, ConfigFile&amp; cf );

protected:
	template&lt;class T&gt; static string T_as_string( const T&amp; t );
	template&lt;class T&gt; static T string_as_T( const string&amp; s );
	static void trim( string&amp; s );

// Exception types
public:
	struct file_not_found {
		string filename;
		file_not_found( const string&amp; filename_ = string() )
			: filename(filename_) {} };
	struct key_not_found {  // thrown only by T read(key) variant of read()
		string key;
		key_not_found( const string&amp; key_ = string() )
			: key(key_) {} };
};

/* static */
template&lt;class T&gt;
string ConfigFile::T_as_string( const T&amp; t )
{
	// Convert from a T to a string
	// Type T must support &lt;&lt; operator
	std::ostringstream ost;
	ost &lt;&lt; t;
	return ost.str();
}

/* static */
template&lt;class T&gt;
T ConfigFile::string_as_T( const string&amp; s )
{
	// Convert from a string to a T
	// Type T must support &gt;&gt; operator
	T t;
	std::istringstream ist(s);
	ist &gt;&gt; t;
	return t;
}

/* static */
template&lt;&gt;
inline string ConfigFile::string_as_T&lt;string&gt;( const string&amp; s )
{
	// Convert from a string to a string
	// In other words, do nothing
	return s;
}

/* static */
template&lt;&gt;
inline bool ConfigFile::string_as_T&lt;bool&gt;( const string&amp; s )
{
	// Convert from a string to a bool
	// Interpret &quot;false&quot;, &quot;F&quot;, &quot;no&quot;, &quot;n&quot;, &quot;0&quot; as false
	// Interpret &quot;true&quot;, &quot;T&quot;, &quot;yes&quot;, &quot;y&quot;, &quot;1&quot;, &quot;-1&quot;, or anything else as true
	bool b = true;
	string sup = s;
	for( string::iterator p = sup.begin(); p != sup.end(); ++p )
		*p = toupper(*p);  // make string all caps
	if( sup==string(&quot;FALSE&quot;) || sup==string(&quot;F&quot;) ||
	    sup==string(&quot;NO&quot;) || sup==string(&quot;N&quot;) ||
	    sup==string(&quot;0&quot;) || sup==string(&quot;NONE&quot;) )
		b = false;
	return b;
}

template&lt;class T&gt;
T ConfigFile::read( const string&amp; key ) const
{
	// Read the value corresponding to key
	mapci p = myContents.find(key);
	if( p == myContents.end() ) throw key_not_found(key);
	return string_as_T&lt;T&gt;( p-&gt;second );
}

template&lt;class T&gt;
T ConfigFile::read( const string&amp; key, const T&amp; value ) const
{
	// Return the value corresponding to key or given default value
	// if key is not found
	mapci p = myContents.find(key);
	if( p == myContents.end() ) return value;
	return string_as_T&lt;T&gt;( p-&gt;second );
}

template&lt;class T&gt;
bool ConfigFile::readInto( T&amp; var, const string&amp; key ) const
{
	// Get the value corresponding to key and store in var
	// Return true if key is found
	// Otherwise leave var untouched
	mapci p = myContents.find(key);
	bool found = ( p != myContents.end() );
	if( found ) var = string_as_T&lt;T&gt;( p-&gt;second );
	return found;
}

template&lt;class T&gt;
bool ConfigFile::readInto( T&amp; var, const string&amp; key, const T&amp; value ) const
{
	// Get the value corresponding to key and store in var
	// Return true if key is found
	// Otherwise set var to given default
	mapci p = myContents.find(key);
	bool found = ( p != myContents.end() );
	if( found )
		var = string_as_T&lt;T&gt;( p-&gt;second );
	else
		var = value;
	return found;
}

template&lt;class T&gt;
void ConfigFile::add( string key, const T&amp; value )
{
	// Add a key with given value
	string v = T_as_string( value );
	trim(key);
	trim(v);
	myContents[key] = v;
	return;
}

#endif  // CONFIGFILE_H
</code></pre>
<p>triplet.h</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct Triplet
{
	int a, b, c;

	Triplet() {}
	Triplet( int u, int v, int w ) : a(u), b(v), c(w) {}
	Triplet( const Triplet&amp; orig ) : a(orig.a), b(orig.b), c(orig.c) {}

	Triplet&amp; operator=( const Triplet&amp; orig )
		{ a = orig.a;  b = orig.b;  c = orig.c;  return *this; }
};

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const Triplet&amp; t )
{
	// Save a triplet to os
	os &lt;&lt; t.a &lt;&lt; &quot; &quot; &lt;&lt; t.b &lt;&lt; &quot; &quot; &lt;&lt; t.c;
	return os;
}

std::istream&amp; operator&gt;&gt;( std::istream&amp; is, Triplet&amp; t )
{
	// Load a triplet from is
	is &gt;&gt; t.a &gt;&gt; t.b &gt;&gt; t.c;
	return is;
}
</code></pre>
<p>Der Autor hat leider kein Beispiel zur Sicherung der veränderten Datei.</p>
<p>Habe dann mit Hilfe von dieser Seite:</p>
<pre><code class="language-cpp">ofstream file;
file.open (&quot;test.conf&quot;);
file &lt;&lt; config;
</code></pre>
<p>, diesen Code zum speichern verwendet.</p>
<p>Mein Problem ist jetzt das in der ursprünglichen Konfigurationsdatei noch Kommentare stehen (#test), die jetzt beim auslesen und überschreiben der Datei verloren gehen. Die Kommentare sollen aber erhalten bleiben, weiß aber nicht wie ich den Code so verändern kann.</p>
<p>Kann mir jemand dabei helfen?</p>
<p>Danke und Gruß<br />
nihilfire</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/249268/config-datei-lesen-schreiben</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 15:27:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/249268.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 05 Sep 2009 08:31:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Config Datei lesen&#x2F;schreiben on Sat, 05 Sep 2009 09:13:55 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Habe auf <a href="http://www-personal.umich.edu/~wagnerr/ConfigFile.html" rel="nofollow">http://www-personal.umich.edu/~wagnerr/ConfigFile.html</a> ein Klasse gefunden die Konfigurationsdateien ließt, verändern und schreiben soll.</p>
<p>ConfigFile.cpp:</p>
<pre><code class="language-cpp">#include &quot;ConfigFile.h&quot;

using std::string;

ConfigFile::ConfigFile( string filename, string delimiter,
                        string comment, string sentry )
	: myDelimiter(delimiter), myComment(comment), mySentry(sentry)
{
	// Construct a ConfigFile, getting keys and values from given file

	std::ifstream in( filename.c_str() );

	if( !in ) throw file_not_found( filename ); 

	in &gt;&gt; (*this);
}

ConfigFile::ConfigFile()
	: myDelimiter( string(1,'=') ), myComment( string(1,'#') )
{
	// Construct a ConfigFile without a file; empty
}

void ConfigFile::remove( const string&amp; key )
{
	// Remove key and its value
	myContents.erase( myContents.find( key ) );
	return;
}

bool ConfigFile::keyExists( const string&amp; key ) const
{
	// Indicate whether key is found
	mapci p = myContents.find( key );
	return ( p != myContents.end() );
}

/* static */
void ConfigFile::trim( string&amp; s )
{
	// Remove leading and trailing whitespace
	static const char whitespace[] = &quot; \n\t\v\r\f&quot;;
	s.erase( 0, s.find_first_not_of(whitespace) );
	s.erase( s.find_last_not_of(whitespace) + 1U );
}

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const ConfigFile&amp; cf )
{
	// Save a ConfigFile to os
	for( ConfigFile::mapci p = cf.myContents.begin();
	     p != cf.myContents.end();
		 ++p )
	{
		os &lt;&lt; p-&gt;first &lt;&lt; &quot; &quot; &lt;&lt; cf.myDelimiter &lt;&lt; &quot; &quot;;
		os &lt;&lt; p-&gt;second &lt;&lt; std::endl;
	}
	return os;
}

std::istream&amp; operator&gt;&gt;( std::istream&amp; is, ConfigFile&amp; cf )
{
	// Load a ConfigFile from is
	// Read in keys and values, keeping internal whitespace
	typedef string::size_type pos;
	const string&amp; delim  = cf.myDelimiter;  // separator
	const string&amp; comm   = cf.myComment;    // comment
	const string&amp; sentry = cf.mySentry;     // end of file sentry
	const pos skip = delim.length();        // length of separator

	string nextline = &quot;&quot;;  // might need to read ahead to see where value ends

	while( is || nextline.length() &gt; 0 )
	{
		// Read an entire line at a time
		string line;
		if( nextline.length() &gt; 0 )
		{
			line = nextline;  // we read ahead; use it now
			nextline = &quot;&quot;;
		}
		else
		{
			std::getline( is, line );
		}

		// Ignore comments
		line = line.substr( 0, line.find(comm) );

		// Check for end of file sentry
		if( sentry != &quot;&quot; &amp;&amp; line.find(sentry) != string::npos ) return is;

		// Parse the line if it contains a delimiter
		pos delimPos = line.find( delim );
		if( delimPos &lt; string::npos )
		{
			// Extract the key
			string key = line.substr( 0, delimPos );
			line.replace( 0, delimPos+skip, &quot;&quot; );

			// See if value continues on the next line
			// Stop at blank line, next line with a key, end of stream,
			// or end of file sentry
			bool terminate = false;
			while( !terminate &amp;&amp; is )
			{
				std::getline( is, nextline );
				terminate = true;

				string nlcopy = nextline;
				ConfigFile::trim(nlcopy);
				if( nlcopy == &quot;&quot; ) continue;

				nextline = nextline.substr( 0, nextline.find(comm) );
				if( nextline.find(delim) != string::npos )
					continue;
				if( sentry != &quot;&quot; &amp;&amp; nextline.find(sentry) != string::npos )
					continue;

				nlcopy = nextline;
				ConfigFile::trim(nlcopy);
				if( nlcopy != &quot;&quot; ) line += &quot;\n&quot;;
				line += nextline;
				terminate = false;
			}

			// Store key and value
			ConfigFile::trim(key);
			ConfigFile::trim(line);
			cf.myContents[key] = line;  // overwrites if key is repeated
		}
	}

	return is;
}
</code></pre>
<p>ConfigFile.h</p>
<pre><code class="language-cpp">// Typical usage
// -------------
// 
// Given a configuration file &quot;settings.inp&quot;:
//   atoms  = 25
//   length = 8.0  # nanometers
//   name = Reece Surcher
// 
// Named values are read in various ways, with or without default values:
//   ConfigFile config( &quot;settings.inp&quot; );
//   int atoms = config.read&lt;int&gt;( &quot;atoms&quot; );
//   double length = config.read( &quot;length&quot;, 10.0 );
//   string author, title;
//   config.readInto( author, &quot;name&quot; );
//   config.readInto( title, &quot;title&quot;, string(&quot;Untitled&quot;) );
// 
// See file example.cpp for more examples.

#ifndef CONFIGFILE_H
#define CONFIGFILE_H

#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;

using std::string;

class ConfigFile {
// Data
protected:
	string myDelimiter;  // separator between key and value
	string myComment;    // separator between value and comments
	string mySentry;     // optional string to signal end of file
	std::map&lt;string,string&gt; myContents;  // extracted keys and values

	typedef std::map&lt;string,string&gt;::iterator mapi;
	typedef std::map&lt;string,string&gt;::const_iterator mapci;

// Methods
public:
	ConfigFile( string filename,
	            string delimiter = &quot;=&quot;,
	            string comment = &quot;#&quot;,
				string sentry = &quot;EndConfigFile&quot; );
	ConfigFile();

	// Search for key and read value or optional default value
	template&lt;class T&gt; T read( const string&amp; key ) const;  // call as read&lt;T&gt;
	template&lt;class T&gt; T read( const string&amp; key, const T&amp; value ) const;
	template&lt;class T&gt; bool readInto( T&amp; var, const string&amp; key ) const;
	template&lt;class T&gt;
	bool readInto( T&amp; var, const string&amp; key, const T&amp; value ) const;

	// Modify keys and values
	template&lt;class T&gt; void add( string key, const T&amp; value );
	void remove( const string&amp; key );

	// Check whether key exists in configuration
	bool keyExists( const string&amp; key ) const;

	// Check or change configuration syntax
	string getDelimiter() const { return myDelimiter; }
	string getComment() const { return myComment; }
	string getSentry() const { return mySentry; }
	string setDelimiter( const string&amp; s )
		{ string old = myDelimiter;  myDelimiter = s;  return old; }  
	string setComment( const string&amp; s )
		{ string old = myComment;  myComment = s;  return old; }

	// Write or read configuration
	friend std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const ConfigFile&amp; cf );
	friend std::istream&amp; operator&gt;&gt;( std::istream&amp; is, ConfigFile&amp; cf );

protected:
	template&lt;class T&gt; static string T_as_string( const T&amp; t );
	template&lt;class T&gt; static T string_as_T( const string&amp; s );
	static void trim( string&amp; s );

// Exception types
public:
	struct file_not_found {
		string filename;
		file_not_found( const string&amp; filename_ = string() )
			: filename(filename_) {} };
	struct key_not_found {  // thrown only by T read(key) variant of read()
		string key;
		key_not_found( const string&amp; key_ = string() )
			: key(key_) {} };
};

/* static */
template&lt;class T&gt;
string ConfigFile::T_as_string( const T&amp; t )
{
	// Convert from a T to a string
	// Type T must support &lt;&lt; operator
	std::ostringstream ost;
	ost &lt;&lt; t;
	return ost.str();
}

/* static */
template&lt;class T&gt;
T ConfigFile::string_as_T( const string&amp; s )
{
	// Convert from a string to a T
	// Type T must support &gt;&gt; operator
	T t;
	std::istringstream ist(s);
	ist &gt;&gt; t;
	return t;
}

/* static */
template&lt;&gt;
inline string ConfigFile::string_as_T&lt;string&gt;( const string&amp; s )
{
	// Convert from a string to a string
	// In other words, do nothing
	return s;
}

/* static */
template&lt;&gt;
inline bool ConfigFile::string_as_T&lt;bool&gt;( const string&amp; s )
{
	// Convert from a string to a bool
	// Interpret &quot;false&quot;, &quot;F&quot;, &quot;no&quot;, &quot;n&quot;, &quot;0&quot; as false
	// Interpret &quot;true&quot;, &quot;T&quot;, &quot;yes&quot;, &quot;y&quot;, &quot;1&quot;, &quot;-1&quot;, or anything else as true
	bool b = true;
	string sup = s;
	for( string::iterator p = sup.begin(); p != sup.end(); ++p )
		*p = toupper(*p);  // make string all caps
	if( sup==string(&quot;FALSE&quot;) || sup==string(&quot;F&quot;) ||
	    sup==string(&quot;NO&quot;) || sup==string(&quot;N&quot;) ||
	    sup==string(&quot;0&quot;) || sup==string(&quot;NONE&quot;) )
		b = false;
	return b;
}

template&lt;class T&gt;
T ConfigFile::read( const string&amp; key ) const
{
	// Read the value corresponding to key
	mapci p = myContents.find(key);
	if( p == myContents.end() ) throw key_not_found(key);
	return string_as_T&lt;T&gt;( p-&gt;second );
}

template&lt;class T&gt;
T ConfigFile::read( const string&amp; key, const T&amp; value ) const
{
	// Return the value corresponding to key or given default value
	// if key is not found
	mapci p = myContents.find(key);
	if( p == myContents.end() ) return value;
	return string_as_T&lt;T&gt;( p-&gt;second );
}

template&lt;class T&gt;
bool ConfigFile::readInto( T&amp; var, const string&amp; key ) const
{
	// Get the value corresponding to key and store in var
	// Return true if key is found
	// Otherwise leave var untouched
	mapci p = myContents.find(key);
	bool found = ( p != myContents.end() );
	if( found ) var = string_as_T&lt;T&gt;( p-&gt;second );
	return found;
}

template&lt;class T&gt;
bool ConfigFile::readInto( T&amp; var, const string&amp; key, const T&amp; value ) const
{
	// Get the value corresponding to key and store in var
	// Return true if key is found
	// Otherwise set var to given default
	mapci p = myContents.find(key);
	bool found = ( p != myContents.end() );
	if( found )
		var = string_as_T&lt;T&gt;( p-&gt;second );
	else
		var = value;
	return found;
}

template&lt;class T&gt;
void ConfigFile::add( string key, const T&amp; value )
{
	// Add a key with given value
	string v = T_as_string( value );
	trim(key);
	trim(v);
	myContents[key] = v;
	return;
}

#endif  // CONFIGFILE_H
</code></pre>
<p>triplet.h</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct Triplet
{
	int a, b, c;

	Triplet() {}
	Triplet( int u, int v, int w ) : a(u), b(v), c(w) {}
	Triplet( const Triplet&amp; orig ) : a(orig.a), b(orig.b), c(orig.c) {}

	Triplet&amp; operator=( const Triplet&amp; orig )
		{ a = orig.a;  b = orig.b;  c = orig.c;  return *this; }
};

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; os, const Triplet&amp; t )
{
	// Save a triplet to os
	os &lt;&lt; t.a &lt;&lt; &quot; &quot; &lt;&lt; t.b &lt;&lt; &quot; &quot; &lt;&lt; t.c;
	return os;
}

std::istream&amp; operator&gt;&gt;( std::istream&amp; is, Triplet&amp; t )
{
	// Load a triplet from is
	is &gt;&gt; t.a &gt;&gt; t.b &gt;&gt; t.c;
	return is;
}
</code></pre>
<p>Der Autor hat leider kein Beispiel zur Sicherung der veränderten Datei.</p>
<p>Habe dann mit Hilfe von dieser Seite:</p>
<pre><code class="language-cpp">ofstream file;
file.open (&quot;test.conf&quot;);
file &lt;&lt; config;
</code></pre>
<p>, diesen Code zum speichern verwendet.</p>
<p>Mein Problem ist jetzt das in der ursprünglichen Konfigurationsdatei noch Kommentare stehen (#test), die jetzt beim auslesen und überschreiben der Datei verloren gehen. Die Kommentare sollen aber erhalten bleiben, weiß aber nicht wie ich den Code so verändern kann.</p>
<p>Kann mir jemand dabei helfen?</p>
<p>Danke und Gruß<br />
nihilfire</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1772553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1772553</guid><dc:creator><![CDATA[nihilfire]]></dc:creator><pubDate>Sat, 05 Sep 2009 09:13:55 GMT</pubDate></item><item><title><![CDATA[Reply to Config Datei lesen&#x2F;schreiben on Sat, 05 Sep 2009 08:51:23 GMT]]></title><description><![CDATA[<p>Wenn eine Config-Date veraendert wird, dann stimmen meist die Kommentare nicht mehr. Warum sollte man die Kommentare also beibehalten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1772561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1772561</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sat, 05 Sep 2009 08:51:23 GMT</pubDate></item><item><title><![CDATA[Reply to Config Datei lesen&#x2F;schreiben on Sat, 05 Sep 2009 08:51:39 GMT]]></title><description><![CDATA[<p>nihilfire schrieb:</p>
<blockquote>
<p>Mein Problem ist jetzt das in der ursprünglichen Konfigurationsdatei noch Kommentare stehen (#test), die jetzt beim auslesen und überschreiben der Datei verloren gehen. Die Kommentare sollen aber erhalten bleiben, weiß aber nicht wie ich den Code so verändern kann.</p>
</blockquote>
<p>Das ist in diesem Code gar nicht vorgesehen.<br />
Und die Kommentare ordentlich und konsistent zu den Werten zu erhalten, ist auch keine einfache Sache. Und auch gar nicht allgemein lösbar. Ich kann nur davon abraten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1772562</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1772562</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sat, 05 Sep 2009 08:51:39 GMT</pubDate></item></channel></rss>