<?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[char wird automatisch in int umgewandelt :(]]></title><description><![CDATA[<p>hi ich hab ne kleine Header-Datei geschrieben, die als Logdatei fungieren soll, funktioniert auch alles, bis auf die beiden Variablen el (endline) und tab (tabulator).</p>
<p>der Header:</p>
<pre><code>#ifndef __LOGFILE__
#define __LOGFILE__
#include &lt;fstream&gt;

using namespace std;

namespace logfile
{

const char tab = '\t';
const char el = '\n';

class Logfile
{
      public:
             Logfile(char* fname) : name(fname) {f.open(name, ios::out);}
             ~Logfile() {f.close();}

             void write(char* str){f&lt;&lt;str&lt;&lt;' ';}
             void write(int i){f&lt;&lt;i&lt;&lt;' ';}
             Logfile&amp; operator&lt;&lt;(char* str){write(str); return *this;}
             Logfile&amp; operator&lt;&lt;(int i){write(i); return *this;}

      private:
              Logfile(const Logfile&amp;);
              const char* name;
              fstream f;
};
}
#endif
</code></pre>
<p>die main:</p>
<pre><code>#include &lt;logfile.h&gt;
using namespace logfile;

int main()
{
    Logfile log(&quot;log.txt&quot;);
    log&lt;&lt;&quot;test&quot;;
    log&lt;&lt;1&lt;&lt;&quot;zahl&quot;&lt;&lt;el;
    log&lt;&lt;&quot;tab&quot;&lt;&lt;tab&lt;&lt;&quot;test&quot;;
    return 0;
}
</code></pre>
<p>und die log.txt:</p>
<pre><code>test 1 zahl 10 tab 9 test
</code></pre>
<p>mich nervt das grad übelst, weil ich nicht weiß, wie ich verhindern kann, dass diese beiden chars in ints umgewandelt werden. ich hab schon rumprobiert:</p>
<p>test ohne andere Wirkung:</p>
<pre><code>Logfile&amp; operator&lt;&lt;(char str){write(str); return *this;}
</code></pre>
<p>die Zeile hab ich noch hinzugefügt, allerdings bleibt alles beim alten.</p>
<p>Danke im voraus für die Rückmeldung!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/306289/char-wird-automatisch-in-int-umgewandelt</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 07:36:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306289.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 23 Jul 2012 22:27:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:32:51 GMT]]></title><description><![CDATA[<p>hi ich hab ne kleine Header-Datei geschrieben, die als Logdatei fungieren soll, funktioniert auch alles, bis auf die beiden Variablen el (endline) und tab (tabulator).</p>
<p>der Header:</p>
<pre><code>#ifndef __LOGFILE__
#define __LOGFILE__
#include &lt;fstream&gt;

using namespace std;

namespace logfile
{

const char tab = '\t';
const char el = '\n';

class Logfile
{
      public:
             Logfile(char* fname) : name(fname) {f.open(name, ios::out);}
             ~Logfile() {f.close();}

             void write(char* str){f&lt;&lt;str&lt;&lt;' ';}
             void write(int i){f&lt;&lt;i&lt;&lt;' ';}
             Logfile&amp; operator&lt;&lt;(char* str){write(str); return *this;}
             Logfile&amp; operator&lt;&lt;(int i){write(i); return *this;}

      private:
              Logfile(const Logfile&amp;);
              const char* name;
              fstream f;
};
}
#endif
</code></pre>
<p>die main:</p>
<pre><code>#include &lt;logfile.h&gt;
using namespace logfile;

int main()
{
    Logfile log(&quot;log.txt&quot;);
    log&lt;&lt;&quot;test&quot;;
    log&lt;&lt;1&lt;&lt;&quot;zahl&quot;&lt;&lt;el;
    log&lt;&lt;&quot;tab&quot;&lt;&lt;tab&lt;&lt;&quot;test&quot;;
    return 0;
}
</code></pre>
<p>und die log.txt:</p>
<pre><code>test 1 zahl 10 tab 9 test
</code></pre>
<p>mich nervt das grad übelst, weil ich nicht weiß, wie ich verhindern kann, dass diese beiden chars in ints umgewandelt werden. ich hab schon rumprobiert:</p>
<p>test ohne andere Wirkung:</p>
<pre><code>Logfile&amp; operator&lt;&lt;(char str){write(str); return *this;}
</code></pre>
<p>die Zeile hab ich noch hinzugefügt, allerdings bleibt alles beim alten.</p>
<p>Danke im voraus für die Rückmeldung!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235060</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235060</guid><dc:creator><![CDATA[Clundsch]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:32:51 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:34:59 GMT]]></title><description><![CDATA[<p>Wenn sie nicht in int umgewandelt werden, wie willst du sie dann ausgeben, wenn du nur int und char* (der übrigens wohl const char* sein sollte) anbietest?</p>
<blockquote>
<p>test ohne andere Wirkung:</p>
<pre><code>Logfile&amp; operator&lt;&lt;(char str){write(str); return *this;}
</code></pre>
<p>Code:</p>
<p>die Zeile hab ich noch hinzugefügt, allerdings bleibt alles beim alten.</p>
</blockquote>
<p>Das verschiebt die gleiche Problematik hin zu deiner write-Funktion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235065</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:34:59 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:36:04 GMT]]></title><description><![CDATA[<p>ja hab ich auch grad gesehen, ich hätte 2 Zeilen dazumachen müssen:</p>
<pre><code>Logfile&amp; operator&lt;&lt;(const char str){write(str); return *this;}
</code></pre>
<p>und</p>
<pre><code>void write(const char str){f&lt;&lt;str&lt;&lt;' ';}
</code></pre>
<p>danke für das mit dem const, hab ich vergessen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235067</guid><dc:creator><![CDATA[Clundsch]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:36:04 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:41:08 GMT]]></title><description><![CDATA[<p>funktioniert jetzt. danke! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
hab auch noch den Code verändert, um minimale Nebenwirkungen zu entfernen:</p>
<p>vorher:</p>
<pre><code>void write(const char str){f&lt;&lt;str&lt;&lt;' ';}
</code></pre>
<p>da kam dann raus:</p>
<pre><code>test 1 zahl 
 tab 	 test
</code></pre>
<p>jetzt:</p>
<pre><code>void write(const char str){f&lt;&lt;str;}
</code></pre>
<p>da kommt jetzt raus:</p>
<pre><code>test 1 zahl 
tab 	test
</code></pre>
<p>Danke für die Hilfe! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235069</guid><dc:creator><![CDATA[Clundsch]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:41:08 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:42:03 GMT]]></title><description><![CDATA[<p>Clundsch schrieb:</p>
<blockquote>
<p>ja hab ich auch grad gesehen, ich hätte 2 Zeilen dazumachen müssen:</p>
<pre><code>Logfile&amp; operator&lt;&lt;(const char str){write(str); return *this;}
</code></pre>
<p>und</p>
<pre><code>void write(const char str){f&lt;&lt;str&lt;&lt;' ';}
</code></pre>
<p>danke für das mit dem const, hab ich vergessen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>Das const so wie hier hingegen ist verwirrend und unüblich. Wen interessieren deine Implementierungsdetails (ob du das str selber veränderst oder nicht) im öffentlichen Interface? Interessant ist doch bei Pointer- und Referenztypen, ob du das dahinter stehende Objekt veränderst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235070</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:42:03 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 22:52:33 GMT]]></title><description><![CDATA[<p>naja, eig. nicht... was wäre denn deiner Meinung nach richtig? (das hier ist bereits die fertige Headerdatei)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235076</guid><dc:creator><![CDATA[Clundsch]]></dc:creator><pubDate>Mon, 23 Jul 2012 22:52:33 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 23:02:45 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">Logfile&amp; operator&lt;&lt;(const char* str){write(str); return *this;}
             Logfile&amp; operator&lt;&lt;(int i){write(i); return *this;}
             Logfile&amp; operator&lt;&lt;(char i){write(i); return *this;}
</code></pre>
<p>Bitte nachvollziehen, auf was sich die const-ness hier bezieht!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235081</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235081</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 23 Jul 2012 23:02:45 GMT</pubDate></item><item><title><![CDATA[Reply to char wird automatisch in int umgewandelt :( on Mon, 23 Jul 2012 23:06:22 GMT]]></title><description><![CDATA[<p>scheint so, als würde das const sich hier lediglich auf den Zeiger beziehen, damit der eigentliche Wert nicht verändert werden kann. Da die anderen beiden Parameter keine Zeiger sind isses da egal... wenns das ist hab ichs verstanden, danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2235082</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2235082</guid><dc:creator><![CDATA[Clundsch]]></dc:creator><pubDate>Mon, 23 Jul 2012 23:06:22 GMT</pubDate></item></channel></rss>