<?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[[solved] fstream schreibt nicht]]></title><description><![CDATA[<p>Hi,<br />
ich hab hier eine Methode, die eine Datei oeffnen soll, eine Zeile hineinschreiben soll und dannach die Datei wieder schliesst. Das Problem ist, dass das genau ein mal funktioniert, und zwar nur dann, wenn die Datei neu angelegt wurde.</p>
<pre><code class="language-cpp">void logLine(string line,string channel) {
	cout &lt;&lt; &quot;logLine ( &quot; &lt;&lt; line &lt;&lt; &quot;, &quot; &lt;&lt; channel &lt;&lt; &quot; )&quot; &lt;&lt; endl;
  bool all = false; // whether we should log to all channels
  string file,timestamp; // some strings
  struct tm *ptr; // pointer
  time_t tm; // time holder
  char hold[10]; // holder
  fstream log; // filestream

  // check if we should be logging to all channels, private messages or one channel
  if (channel == &quot;&quot;) { 
    // we got passed a null, so log to all channels
    all = true;
  } else if ((channel != LOG_PRIVMSG) &amp;&amp; (!isIn(channel))) {
    // we got passed a channel we shouldnt log, so dont ;)
	cout &lt;&lt; &quot;else if ((channel != LOG_PRIVMSG) &amp;&amp; (!isIn(channel)))&quot; &lt;&lt; endl;
    return;
  }

  // loop through the channels
  for (int i = 0;i &lt; MAX_CHANNELS;i++) {

    // check what we should be logging to
    if (all) { file = LOG_CHANNELS[i]; }
    else if (channel == LOG_PRIVMSG) { file = string(ME.nick); }
    else { file = channel; }

    // check if we have a valid looking file to log to
    if (file == &quot;&quot;) { cout &lt;&lt; &quot;file == \&quot;\&quot;&quot; &lt;&lt; endl; break; }

    // if we have a filename with a # at the front, strip it off
    if (file.find(&quot;#&quot;,0) == 0) { file.erase(0,1); }

    // lowercase the filename
    transform (file.begin(),file.end(), file.begin(), static_cast&lt;int (*)(int)&gt;(tolower));

    // get the current time
    tm = time(NULL);
    // get the local time
    ptr = localtime(&amp;tm);
    // get a nicely formatted time
    strftime(hold ,10 , &quot;[%R] &quot;,ptr);
    // save it as timestamp
    timestamp = hold;
    // get a nicely formated date
    strftime(hold ,10 , &quot;%Y%m%d&quot;,ptr);
    // build the filename
    file = LOG_DIR + file + LOG_MID + hold;

    // check if the filename exists
    if (fileExists(file)) {
      // file exists, append
      log.open(file.c_str(),ios::app);
      cout &lt;&lt; &quot;ios:app&quot; &lt;&lt; endl;
    } else {
      // new file, open at the beginning
      log.open(file.c_str(),ios::out);
      cout &lt;&lt; &quot;ios::out&quot; &lt;&lt; endl;
    }
    // log the entry to the open file
    log &lt;&lt; timestamp &lt;&lt; line &lt;&lt; endl;  
    cout &lt;&lt; timestamp &lt;&lt; &quot;||&quot; &lt;&lt; line &lt;&lt; endl;
    // close the file
    log.close();
    // if we are only doing 1 channel, break now
    if (!all) { break; }
  } // end of for looping through channels
}
</code></pre>
<p>Die Funktion habe ich nicht selber geschrieben, ich stelle die Datei gerade auf eine neue Version des Hauptprogramms um. Die Ausgabe sagt mir dass eigentlich alles funktioniert, existiert die Datei, wird &quot;ios::app&quot; ausgegeben, existiert sie nciht, wird &quot;ios::out&quot; ausgegeben. in timestamp und line (direkt nach dem schreiben in den stream) stehen die korrekten Strings drin. Ich kann keinen Fehler erkennen, die Datei wird nur von dieser Funktion benutzt.<br />
An sich wurde diese Funktion nicht veraendert, mit der alten Version des Programms hat sie offenbar funktioniert.<br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/211346/solved-fstream-schreibt-nicht</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 00:08:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/211346.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 22 Apr 2008 10:00:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [solved] fstream schreibt nicht on Tue, 22 Apr 2008 13:27:26 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich hab hier eine Methode, die eine Datei oeffnen soll, eine Zeile hineinschreiben soll und dannach die Datei wieder schliesst. Das Problem ist, dass das genau ein mal funktioniert, und zwar nur dann, wenn die Datei neu angelegt wurde.</p>
<pre><code class="language-cpp">void logLine(string line,string channel) {
	cout &lt;&lt; &quot;logLine ( &quot; &lt;&lt; line &lt;&lt; &quot;, &quot; &lt;&lt; channel &lt;&lt; &quot; )&quot; &lt;&lt; endl;
  bool all = false; // whether we should log to all channels
  string file,timestamp; // some strings
  struct tm *ptr; // pointer
  time_t tm; // time holder
  char hold[10]; // holder
  fstream log; // filestream

  // check if we should be logging to all channels, private messages or one channel
  if (channel == &quot;&quot;) { 
    // we got passed a null, so log to all channels
    all = true;
  } else if ((channel != LOG_PRIVMSG) &amp;&amp; (!isIn(channel))) {
    // we got passed a channel we shouldnt log, so dont ;)
	cout &lt;&lt; &quot;else if ((channel != LOG_PRIVMSG) &amp;&amp; (!isIn(channel)))&quot; &lt;&lt; endl;
    return;
  }

  // loop through the channels
  for (int i = 0;i &lt; MAX_CHANNELS;i++) {

    // check what we should be logging to
    if (all) { file = LOG_CHANNELS[i]; }
    else if (channel == LOG_PRIVMSG) { file = string(ME.nick); }
    else { file = channel; }

    // check if we have a valid looking file to log to
    if (file == &quot;&quot;) { cout &lt;&lt; &quot;file == \&quot;\&quot;&quot; &lt;&lt; endl; break; }

    // if we have a filename with a # at the front, strip it off
    if (file.find(&quot;#&quot;,0) == 0) { file.erase(0,1); }

    // lowercase the filename
    transform (file.begin(),file.end(), file.begin(), static_cast&lt;int (*)(int)&gt;(tolower));

    // get the current time
    tm = time(NULL);
    // get the local time
    ptr = localtime(&amp;tm);
    // get a nicely formatted time
    strftime(hold ,10 , &quot;[%R] &quot;,ptr);
    // save it as timestamp
    timestamp = hold;
    // get a nicely formated date
    strftime(hold ,10 , &quot;%Y%m%d&quot;,ptr);
    // build the filename
    file = LOG_DIR + file + LOG_MID + hold;

    // check if the filename exists
    if (fileExists(file)) {
      // file exists, append
      log.open(file.c_str(),ios::app);
      cout &lt;&lt; &quot;ios:app&quot; &lt;&lt; endl;
    } else {
      // new file, open at the beginning
      log.open(file.c_str(),ios::out);
      cout &lt;&lt; &quot;ios::out&quot; &lt;&lt; endl;
    }
    // log the entry to the open file
    log &lt;&lt; timestamp &lt;&lt; line &lt;&lt; endl;  
    cout &lt;&lt; timestamp &lt;&lt; &quot;||&quot; &lt;&lt; line &lt;&lt; endl;
    // close the file
    log.close();
    // if we are only doing 1 channel, break now
    if (!all) { break; }
  } // end of for looping through channels
}
</code></pre>
<p>Die Funktion habe ich nicht selber geschrieben, ich stelle die Datei gerade auf eine neue Version des Hauptprogramms um. Die Ausgabe sagt mir dass eigentlich alles funktioniert, existiert die Datei, wird &quot;ios::app&quot; ausgegeben, existiert sie nciht, wird &quot;ios::out&quot; ausgegeben. in timestamp und line (direkt nach dem schreiben in den stream) stehen die korrekten Strings drin. Ich kann keinen Fehler erkennen, die Datei wird nur von dieser Funktion benutzt.<br />
An sich wurde diese Funktion nicht veraendert, mit der alten Version des Programms hat sie offenbar funktioniert.<br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496088</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496088</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Tue, 22 Apr 2008 13:27:26 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] fstream schreibt nicht on Tue, 22 Apr 2008 12:33:36 GMT]]></title><description><![CDATA[<p>also beim ersten durchgehen ist mir folgendes aufgefallen:</p>
<p>C167 schrieb:</p>
<blockquote>
<p>dass das genau ein mal funktioniert, und zwar nur dann, wenn die Datei neu angelegt wurde</p>
</blockquote>
<p>Das könnte daran liegen, dass hier:</p>
<p>C167 schrieb:</p>
<blockquote>
<pre><code class="language-cpp">log.open(file.c_str(),ios::app);
</code></pre>
</blockquote>
<p>auch noch ein ios::out stehen muss, also</p>
<pre><code class="language-cpp">log.open(file.c_str(), ios::out|ios::app);
</code></pre>
<p>( ursprünglicher Code funktioniert also nur einmal, da beim ersten Mal nicht diese Funktion aufgerufen wird, sondern die darunter stehende( zumindest gehe ich davon aus <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="🙂"
    /> ), welche ja &quot;ios::out&quot; ausführt )<br />
Außerdem könnte man versuchen mit &quot;log.clear()&quot; die Objektinternen Statusbits zu löschen, ob das hilft, ist aber nicht gewiss <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/1496164</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496164</guid><dc:creator><![CDATA[double *T]]></dc:creator><pubDate>Tue, 22 Apr 2008 12:33:36 GMT</pubDate></item><item><title><![CDATA[Reply to [solved] fstream schreibt nicht on Tue, 22 Apr 2008 13:27:09 GMT]]></title><description><![CDATA[<p>vielen Dank, genau das war es. Ich hab ios::out dazugehaengt und schon gings los!<br />
mfg<br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496192</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496192</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Tue, 22 Apr 2008 13:27:09 GMT</pubDate></item></channel></rss>