<?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[fstream: wann open&#x2F;close?]]></title><description><![CDATA[<p>Hallo!</p>
<p>Sollte man bei Zugriffen auf eine Datei via fstream, diese Datei nur kurzfristig wenn eine Dateioperation ansteht öffnen und schließen oder einmalig öffnen und nach Programmende wieder schließen (wenn die Datei nur von diesem Programm genutzt wird).</p>
<pre><code class="language-cpp">class File{

public:
    File(const std::string&amp; fileName)
        : m_file(fileName.c_str(), std::ios_base::out | std::ios_base::app)
    {
        if( !m_file.is_open() ) throw FileError();
    }

    void Write(const std::string&amp; msg){ m_file &lt;&lt; msg &lt;&lt; std::endl; };

private:
    fstream m_file;
};
</code></pre>
<pre><code class="language-cpp">class File{

public:
    File(const std::string&amp; fileName)
        : m_fileName(fileName)
    {
    }

    void Write(const std::string&amp; msg){

        std::fstream file(m_fileName.c_str(), std::ios_base::out | std::ios_base::app);
        if( !m_file.is_open() ) throw FileError();

        file &lt;&lt; msg &lt;&lt; std::endl; };
        // file.close automatisch durch fstream-dtor
    }

private:
    std::stream m_fileName;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/242320/fstream-wann-open-close</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 03:19:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242320.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Jun 2009 13:54:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to fstream: wann open&#x2F;close? on Tue, 02 Jun 2009 13:54:34 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Sollte man bei Zugriffen auf eine Datei via fstream, diese Datei nur kurzfristig wenn eine Dateioperation ansteht öffnen und schließen oder einmalig öffnen und nach Programmende wieder schließen (wenn die Datei nur von diesem Programm genutzt wird).</p>
<pre><code class="language-cpp">class File{

public:
    File(const std::string&amp; fileName)
        : m_file(fileName.c_str(), std::ios_base::out | std::ios_base::app)
    {
        if( !m_file.is_open() ) throw FileError();
    }

    void Write(const std::string&amp; msg){ m_file &lt;&lt; msg &lt;&lt; std::endl; };

private:
    fstream m_file;
};
</code></pre>
<pre><code class="language-cpp">class File{

public:
    File(const std::string&amp; fileName)
        : m_fileName(fileName)
    {
    }

    void Write(const std::string&amp; msg){

        std::fstream file(m_fileName.c_str(), std::ios_base::out | std::ios_base::app);
        if( !m_file.is_open() ) throw FileError();

        file &lt;&lt; msg &lt;&lt; std::endl; };
        // file.close automatisch durch fstream-dtor
    }

private:
    std::stream m_fileName;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1719672</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1719672</guid><dc:creator><![CDATA[Roger Wilco]]></dc:creator><pubDate>Tue, 02 Jun 2009 13:54:34 GMT</pubDate></item><item><title><![CDATA[Reply to fstream: wann open&#x2F;close? on Tue, 02 Jun 2009 13:58:12 GMT]]></title><description><![CDATA[<p>Auf gar keinen Fall zwischen den Zugriffen öffnen und schließen! Das ist dann fürchterlich langsam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1719678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1719678</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jun 2009 13:58:12 GMT</pubDate></item><item><title><![CDATA[Reply to fstream: wann open&#x2F;close? on Tue, 02 Jun 2009 14:01:01 GMT]]></title><description><![CDATA[<p>Habe ich Nachteile, wenn ich die Datei während der Programmausführung durchgehend geöffnet halte?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1719682</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1719682</guid><dc:creator><![CDATA[Roger Wilco]]></dc:creator><pubDate>Tue, 02 Jun 2009 14:01:01 GMT</pubDate></item><item><title><![CDATA[Reply to fstream: wann open&#x2F;close? on Tue, 02 Jun 2009 14:04:40 GMT]]></title><description><![CDATA[<p>Roger Wilco schrieb:</p>
<blockquote>
<p>Habe ich Nachteile, wenn ich die Datei während der Programmausführung durchgehend geöffnet halte?</p>
</blockquote>
<p>Du brauchst wahrscheinlich ein paar zusätzliche Systemressourcen. Aber in praktischer Hinsicht, sofern niemand anderes das File öffnen muss, nicht wirklich. Auf die Ausführungsgeschwindigkeit hat es ziemlich sicher keinen Einfluss.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1719687</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1719687</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Tue, 02 Jun 2009 14:04:40 GMT</pubDate></item><item><title><![CDATA[Reply to fstream: wann open&#x2F;close? on Tue, 02 Jun 2009 14:07:15 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<p>Danke für die Hilfe, Antwort oder Meinung!</p>
</blockquote>
<p>DITO <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1719691</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1719691</guid><dc:creator><![CDATA[Roger Wilco]]></dc:creator><pubDate>Tue, 02 Jun 2009 14:07:15 GMT</pubDate></item></channel></rss>