<?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[std::ifstream und eingabe...]]></title><description><![CDATA[<p>Vorgeschichte:<br />
Also ich habe ein Projekt, was verschiedene Einstellungen aus einer Datei laden soll (hatte zu erst ne *.ini genommen und mit der WinAPI gearbeitet - aber hatte dann paar Dinge, die damit nur sehr umständlich zu bewerkstelligen sind und manche, die dem *.ini - Standard ne entsprechen)...</p>
<p>Problem:<br />
Auf jeden Fall schreib ich mir gerad selbst eine Klasse, die mir meine *.cfg-Datei ausliest - soll aber sowohl Kategorieen als auch Werte reinschreiben, wenn die noch nicht drinstehen... Und ehrlich gesagt hab ich von Streams nicht sooo viel Ahnung... ^^</p>
<p>Lösungsansatz:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;fstream&gt;
#include &quot;explode.hpp&quot;

namespace Nini
	{
		const std::string filename (&quot;.\\data.cfg&quot;);
	};

class Tini
	{
		private: //VAR
			std::ifstream *m_stream;
			const std::string m_filename;
			std::string m_actcat_string;
			std::streamoff m_actcat_streamoff; //long

			bool m_iscomment;
/*NOT USED*/bool m_isquotet;
		private: //FKT
			void Rewind (void)
				{
					m_stream-&gt;seekg (0, std::ios_base::beg);
					m_iscomment = false;
					m_isquotet = false;
				};

			bool search_cat (const std::string &amp;_catname)
				{
//unwichtig
				};
		public:
			Tini (const std::string &amp;_fn = Nini::filename) : m_filename (_fn)
				{
//stimmt das eigtl? ein und ausgabe (also exklusiver zugriff) und nicht neu erstellen - oder hab ich die parameter da falsch gewählt?
					m_stream = new std::ifstream (m_filename.c_str (), std::ios::out | std::ios::in | std::ios::app); //ja - ich weiß, initialisierungsliste etc... aber so is übersichtlicher find ich :P
				};
			~Tini (void)
				{
					delete m_stream;
				};
			void SetCat (const std::string &amp;_catname) //Kategorie setzen - zur Not eben reinschreiben
				{
					Rewind ();
					if (!search_cat (_catname))
						AddCat (_catname);
					m_actcat_streamoff = m_stream-&gt;tellg ();
					m_actcat_string = _catname;
				};
			void AddCat (const std::string &amp;_newcatname)
				{ // HIER ist das Problem...
					m_stream-&gt;seekg (-2, std::ios_base::end);
					char _i[2];
					*m_stream &gt;&gt; _i;
					if ((_i[0] != '\r') || (_i[1] != '\n'))
						*m_stream &lt;&lt; &quot;\r\n&quot;;
/*guckt, ob nen Zeilenumbruch am Ende der Datei ist
Und SOLLTE dann eben Daten in die Datei reinschreiben
*/
					*m_stream &lt;&lt; &quot;\t\t[&quot; + _newcatname + &quot;]&quot; &lt;&lt; std::endl;
				};
/*etc...*/
	};
</code></pre>
<p>Jetzt meine konkrete <strong>Frage</strong>:<br />
Kann ich den gleichen Stream benutzen (und nur iwie verändern oder was würdet ihr mir raten? ^^)</p>
<p>Danke schon mal...<br />
Byebye <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/206630/std-ifstream-und-eingabe</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 14:48:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/206630.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Feb 2008 00:36:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::ifstream und eingabe... on Wed, 27 Feb 2008 00:44:10 GMT]]></title><description><![CDATA[<p>Vorgeschichte:<br />
Also ich habe ein Projekt, was verschiedene Einstellungen aus einer Datei laden soll (hatte zu erst ne *.ini genommen und mit der WinAPI gearbeitet - aber hatte dann paar Dinge, die damit nur sehr umständlich zu bewerkstelligen sind und manche, die dem *.ini - Standard ne entsprechen)...</p>
<p>Problem:<br />
Auf jeden Fall schreib ich mir gerad selbst eine Klasse, die mir meine *.cfg-Datei ausliest - soll aber sowohl Kategorieen als auch Werte reinschreiben, wenn die noch nicht drinstehen... Und ehrlich gesagt hab ich von Streams nicht sooo viel Ahnung... ^^</p>
<p>Lösungsansatz:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;fstream&gt;
#include &quot;explode.hpp&quot;

namespace Nini
	{
		const std::string filename (&quot;.\\data.cfg&quot;);
	};

class Tini
	{
		private: //VAR
			std::ifstream *m_stream;
			const std::string m_filename;
			std::string m_actcat_string;
			std::streamoff m_actcat_streamoff; //long

			bool m_iscomment;
/*NOT USED*/bool m_isquotet;
		private: //FKT
			void Rewind (void)
				{
					m_stream-&gt;seekg (0, std::ios_base::beg);
					m_iscomment = false;
					m_isquotet = false;
				};

			bool search_cat (const std::string &amp;_catname)
				{
//unwichtig
				};
		public:
			Tini (const std::string &amp;_fn = Nini::filename) : m_filename (_fn)
				{
//stimmt das eigtl? ein und ausgabe (also exklusiver zugriff) und nicht neu erstellen - oder hab ich die parameter da falsch gewählt?
					m_stream = new std::ifstream (m_filename.c_str (), std::ios::out | std::ios::in | std::ios::app); //ja - ich weiß, initialisierungsliste etc... aber so is übersichtlicher find ich :P
				};
			~Tini (void)
				{
					delete m_stream;
				};
			void SetCat (const std::string &amp;_catname) //Kategorie setzen - zur Not eben reinschreiben
				{
					Rewind ();
					if (!search_cat (_catname))
						AddCat (_catname);
					m_actcat_streamoff = m_stream-&gt;tellg ();
					m_actcat_string = _catname;
				};
			void AddCat (const std::string &amp;_newcatname)
				{ // HIER ist das Problem...
					m_stream-&gt;seekg (-2, std::ios_base::end);
					char _i[2];
					*m_stream &gt;&gt; _i;
					if ((_i[0] != '\r') || (_i[1] != '\n'))
						*m_stream &lt;&lt; &quot;\r\n&quot;;
/*guckt, ob nen Zeilenumbruch am Ende der Datei ist
Und SOLLTE dann eben Daten in die Datei reinschreiben
*/
					*m_stream &lt;&lt; &quot;\t\t[&quot; + _newcatname + &quot;]&quot; &lt;&lt; std::endl;
				};
/*etc...*/
	};
</code></pre>
<p>Jetzt meine konkrete <strong>Frage</strong>:<br />
Kann ich den gleichen Stream benutzen (und nur iwie verändern oder was würdet ihr mir raten? ^^)</p>
<p>Danke schon mal...<br />
Byebye <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1463703</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1463703</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 27 Feb 2008 00:44:10 GMT</pubDate></item><item><title><![CDATA[Reply to std::ifstream und eingabe... on Wed, 27 Feb 2008 08:41:49 GMT]]></title><description><![CDATA[<p>Wie wäre es, wenn Du die komplette Datei einliest, sie im Speicher verwaltest (z.B. per map&lt;string, map&lt;string, string&gt; &gt;) und am ende wieder zurückschreibst?</p>
<p>Das wäre sicher eine einfachere Handhabung als in der Datei hin und her zu springen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1463776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1463776</guid><dc:creator><![CDATA[Airdamn]]></dc:creator><pubDate>Wed, 27 Feb 2008 08:41:49 GMT</pubDate></item><item><title><![CDATA[Reply to std::ifstream und eingabe... on Wed, 27 Feb 2008 08:59:05 GMT]]></title><description><![CDATA[<p>hab ich auch schon überlegt (und angefangen gehabt bevor ich es mir dann doch wieder anders überlegt hab und das ganze noch ma neu gemacht hab ^^) - aber mich aus irgendwelchen gründen dann doch anders entschieden gehabt ^^ mir fällt gerad nicht ma mehr ein, warum -.-<br />
naja... dann mach ich es wohl doch so <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f623.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--persevering_face"
      title="&gt;.&lt;"
      alt="😣"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1463783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1463783</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 27 Feb 2008 08:59:05 GMT</pubDate></item></channel></rss>