<?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[[GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray)]]></title><description><![CDATA[<p>Morgen,</p>
<p>ich bastel mir gerade einen ganz simplen Parser, der aus einer Datei folgender Bauweise ein Stringarray erzeugt, das Variablenname und Variablenwert ausließt.</p>
<pre><code>Var Wert
Test Muster
</code></pre>
<p>field.h</p>
<pre><code class="language-cpp">#ifndef READ_CONFIG_H
#define READ_CONFIG_H

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

class Files
{
    // VARIABLEN
    std::string vars_[64][2];   // max. 64 lines, 2 states: varname and value
    const char* filename_;
    std::fstream file_;
    int items_;

    public:
        // GETTERS
        const char* getVar_(const char* var);

        // SETTERS
        void  setVar_(const char* var, const char* value);

        // COSTRUCTORS
        Files(const char* filename, bool &amp;proof);
       ~Files();

    private:
        // FUNCTIONS
        void write_();
        void add_();

};

#endif
</code></pre>
<p>Der Konstruktor sieht hierbei folgendermaßen aus:</p>
<pre><code class="language-cpp">Files::Files(const char* filename, bool &amp;proof)
{
    // VARIABLES
    filename_   = new char[255];
    filename_   = filename;
    items_      = 0;        // index for loop

    char* c     = new char[255];
    bool  b     = 0;        // index (varname or value?)

    file_.open(filename_,ios::in);    // open file

    if ( file_.good() )
    {
        cout &lt;&lt; &quot;Datei geladen: &quot; &lt;&lt; filename_ &lt;&lt; endl;

        while ( ! file_.eof() )
        {
            // Read and Save varname

            file_ &gt;&gt; c;
            if ( ! file_.eof() )
            {
                vars_[items_][b] = c;

                if ( b == 0 )
                {
                    b = 1;
                } else {
                    b = 0;
                    items_++;
                }

                cout &lt;&lt; vars_[items_][0] &lt;&lt; &quot;:&quot; &lt;&lt; vars_[items_][1] &lt;&lt; endl;
            }
        }

        proof = true;

    } else {
        cout &lt;&lt; &quot;Could not open &quot; &lt;&lt; filename_ &lt;&lt; &quot;!&quot; &lt;&lt; endl;
        this-&gt;~Files();

        proof = false;
    }

    file_.close();
}
</code></pre>
<p>Lege ich nun ein Objekt an, dann wird mir an der Stelle von</p>
<pre><code class="language-cpp">cout &lt;&lt; vars_[items_][0] &lt;&lt; &quot;:&quot; &lt;&lt; vars_[items_][1] &lt;&lt; endl;
</code></pre>
<p>folgendes ausgegeben:</p>
<blockquote>
<p>Var:<br />
:<br />
test:<br />
:</p>
</blockquote>
<p>Dennoch wird die Datei richtig geschrieben:</p>
<pre><code class="language-cpp">void Files::write_()
{
    file_.open(filename_,ios::out);    // open file

    for( int i = 0; i &lt;= items_; i++ )
    {
        file_ &lt;&lt; vars_[i][0] &lt;&lt; &quot; &quot; &lt;&lt; vars_[i][1] &lt;&lt; endl;
    }

    file_.close();
}
</code></pre>
<p>(Die Funktion wird auch im Destruktor aufgerufen)</p>
<p>Ich weiß nicht mehr weiter und bitte um Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/291145/gelöst-probleme-beim-einlesen-ausgeben-fstream-stringarray</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 13:03:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/291145.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 Aug 2011 14:52:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 19:00:40 GMT]]></title><description><![CDATA[<p>Morgen,</p>
<p>ich bastel mir gerade einen ganz simplen Parser, der aus einer Datei folgender Bauweise ein Stringarray erzeugt, das Variablenname und Variablenwert ausließt.</p>
<pre><code>Var Wert
Test Muster
</code></pre>
<p>field.h</p>
<pre><code class="language-cpp">#ifndef READ_CONFIG_H
#define READ_CONFIG_H

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

class Files
{
    // VARIABLEN
    std::string vars_[64][2];   // max. 64 lines, 2 states: varname and value
    const char* filename_;
    std::fstream file_;
    int items_;

    public:
        // GETTERS
        const char* getVar_(const char* var);

        // SETTERS
        void  setVar_(const char* var, const char* value);

        // COSTRUCTORS
        Files(const char* filename, bool &amp;proof);
       ~Files();

    private:
        // FUNCTIONS
        void write_();
        void add_();

};

#endif
</code></pre>
<p>Der Konstruktor sieht hierbei folgendermaßen aus:</p>
<pre><code class="language-cpp">Files::Files(const char* filename, bool &amp;proof)
{
    // VARIABLES
    filename_   = new char[255];
    filename_   = filename;
    items_      = 0;        // index for loop

    char* c     = new char[255];
    bool  b     = 0;        // index (varname or value?)

    file_.open(filename_,ios::in);    // open file

    if ( file_.good() )
    {
        cout &lt;&lt; &quot;Datei geladen: &quot; &lt;&lt; filename_ &lt;&lt; endl;

        while ( ! file_.eof() )
        {
            // Read and Save varname

            file_ &gt;&gt; c;
            if ( ! file_.eof() )
            {
                vars_[items_][b] = c;

                if ( b == 0 )
                {
                    b = 1;
                } else {
                    b = 0;
                    items_++;
                }

                cout &lt;&lt; vars_[items_][0] &lt;&lt; &quot;:&quot; &lt;&lt; vars_[items_][1] &lt;&lt; endl;
            }
        }

        proof = true;

    } else {
        cout &lt;&lt; &quot;Could not open &quot; &lt;&lt; filename_ &lt;&lt; &quot;!&quot; &lt;&lt; endl;
        this-&gt;~Files();

        proof = false;
    }

    file_.close();
}
</code></pre>
<p>Lege ich nun ein Objekt an, dann wird mir an der Stelle von</p>
<pre><code class="language-cpp">cout &lt;&lt; vars_[items_][0] &lt;&lt; &quot;:&quot; &lt;&lt; vars_[items_][1] &lt;&lt; endl;
</code></pre>
<p>folgendes ausgegeben:</p>
<blockquote>
<p>Var:<br />
:<br />
test:<br />
:</p>
</blockquote>
<p>Dennoch wird die Datei richtig geschrieben:</p>
<pre><code class="language-cpp">void Files::write_()
{
    file_.open(filename_,ios::out);    // open file

    for( int i = 0; i &lt;= items_; i++ )
    {
        file_ &lt;&lt; vars_[i][0] &lt;&lt; &quot; &quot; &lt;&lt; vars_[i][1] &lt;&lt; endl;
    }

    file_.close();
}
</code></pre>
<p>(Die Funktion wird auch im Destruktor aufgerufen)</p>
<p>Ich weiß nicht mehr weiter und bitte um Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104927</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 11 Aug 2011 19:00:40 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 15:03:56 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">} else {
                    b = 0;
                    items_++;
                }

                cout &lt;&lt; vars_[items_][0] &lt;&lt; &quot;:&quot; &lt;&lt; vars_[items_][1] &lt;&lt; endl;
</code></pre>
<p>items_ ist jetzt schon auf dem nächsten Element.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104934</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 11 Aug 2011 15:03:56 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 15:23:24 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>items_ ist jetzt schon auf dem nächsten Element.</p>
</blockquote>
<p>Autsch, das tut nur selbst weh. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
<p>Dann liegt das eigentliche Problem vorbeigeschossen und es liegt in der const char* File::getVar_()</p>
<pre><code class="language-cpp">const char* Files::getVar_(const char* var)
{
    for ( int i; i &lt; items_; i++ )
    {
        if ( var == ( vars_[i][0].c_str() ) )
        {
            return vars_[i][1].c_str();
        }
    }

    return &quot;&quot;;
}
</code></pre>
<p>Wenn ich diese Funktion nämlich mit</p>
<pre><code class="language-cpp">Files config( CONFIGFILE, test );

// CODE (Überprüfung der bool test

cout &lt;&lt; config.getVar_(&quot;test&quot;) &lt;&lt; endl;
</code></pre>
<p>aufrufe, erhalte ich eine Leerzeile in der Ausgabe bzw. den String, der zurückgegeben werden soll, wenn keine Übereinstimmung gefunden wird, obwohl vars_[1][0] in diesem Fall &quot;test&quot; betragen müsste.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104938</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104938</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 11 Aug 2011 15:23:24 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 15:27:31 GMT]]></title><description><![CDATA[<p>Schon schlimm, was manche als C++ bezeichnen. Hierfür würde ich einen std::ifstream nehmen und die Key-Value Paare in eine std::map&lt;std::string, std::string&gt; speichern. In etwa so (ungetestet):</p>
<pre><code class="language-cpp">class property_reader
{
    std::map&lt;std::string, std::string&gt; properties;

public:

    const std::string filename;

    property_reader(std::string filename)
        : filename(filename)
    {
        std::ifstream is(filename.c_str());

        std::string line;
        while(std::getline(is, line))
        {
            size_t i = line.find(' ');
            properties.insert(std::make_pair(line.substr(0, i), line.substr(i)));
        }
    }

    std::string get(const std::string&amp; property) const
    {
        auto i = properties.find(property);

        if(i != properties.end)
            return i-&gt;second;

        throw not_found_error(property);
    }

    void set(std::string key, std::string value)
    {
        map.insert(std::make_pair(std::move(key), std::move(value));
    }

    ~property_reader()
    {
        std::ofstream os(filename.c_str());

        for(auto i = properties.begin(); i != properties.end(); ++i)
            os &lt;&lt; i-&gt;first &lt;&lt; ' ' &lt;&lt; i-&gt;second &lt;&lt; '\n';
    }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2104946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104946</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Thu, 11 Aug 2011 15:27:31 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 15:59:50 GMT]]></title><description><![CDATA[<p>Daran hatte ich gar nicht gedacht. Ich werde es mal probieren, danke.</p>
<p>EDIT:</p>
<p>Was hat es mit den auto-Variablen auf sich?<br />
Der Compiler gibt mir die Fehlermeldung aus, dass diese nicht ohne Dateityp deklariert werden können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104950</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 11 Aug 2011 15:59:50 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 16:10:20 GMT]]></title><description><![CDATA[<p>Ki schrieb:</p>
<blockquote>
<p>Was hat es mit den auto-Variablen auf sich?<br />
Der Compiler gibt mir die Fehlermeldung aus, dass diese nicht ohne Dateityp deklariert werden können.</p>
</blockquote>
<p>In C++03 bezeichnete es IM(H)O &quot;RAII&quot;, deshalb brauchst du auch einen Datentyp. Im kommenden Standard bedeutet es die Ableitung des Datentyps durch seinen Initialisierungswert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104963</guid><dc:creator><![CDATA[EOutOfResources]]></dc:creator><pubDate>Thu, 11 Aug 2011 16:10:20 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 16:30:26 GMT]]></title><description><![CDATA[<p>und welcher Datentyp muss das in diesem Fall sein?<br />
Wenn ich die Variable mit</p>
<pre><code class="language-cpp">std::map&lt;std::string,std::string&gt;::iterator in;
</code></pre>
<p>deklariere, wie heir (<a href="http://www.cplusplus.com/reference/stl/map/find/" rel="nofollow">http://www.cplusplus.com/reference/stl/map/find/</a>) beschrieben, dann gibt es haufenweise no-match-Fehlermeldungen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2104967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2104967</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 11 Aug 2011 16:30:26 GMT</pubDate></item><item><title><![CDATA[Reply to [GELÖST] Probleme beim Einlesen&#x2F;Ausgeben (fstream, Stringarray) on Thu, 11 Aug 2011 19:00:19 GMT]]></title><description><![CDATA[<p>Thema erledigt, Problem gelöst. Habe den Code nach einigen Schwierigkeiten folgendermaßen umgeschrieben und es funktioniert wunderbar:</p>
<p>reader.h</p>
<pre><code class="language-cpp">#ifndef READER_H
#define READER_H

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

class Reader
{
    // VARIABLES
    std::map&lt;std::string, std::string&gt; items_;
    const std::string filename_; 

    public:
        // CONSTRUCTORS
        Reader(std::string filename);
        ~Reader();

        // GETTERS
        std::string get(const std::string&amp; property) const;

        // SETTERS
        void set(std::string key, std::string value);

        // FUNCTIONS
        void write();

};

#endif
</code></pre>
<p>reader.cpp</p>
<pre><code class="language-cpp">#include &quot;reader.h&quot;

Reader::Reader(std::string filename)
    : filename_(filename)
{
    std::ifstream is(filename_.c_str());

    std::string line;

    while(std::getline(is, line))
    {
        size_t i = line.find(' ');

        items_.insert(std::make_pair(line.substr(0, i), line.substr(i).erase(0,1)));
    }
}

std::string Reader::get(const std::string&amp; item) const
{
    std::map&lt;std::string, std::string&gt;::const_iterator in = items_.find(item);

    if(in != items_.end())
    {
        return in-&gt;second;
    }

    return &quot;&quot;;
}

void Reader::set(std::string key, std::string value)
{
    items_.insert(std::make_pair( key , value ) );
}

void Reader::write()
{
    std::ofstream os(filename_.c_str());

    for( std::map&lt;std::string, std::string&gt;::iterator in = items_.begin(); in != items_.end(); ++in )
    {
        os &lt;&lt; in-&gt;first &lt;&lt; ' ' &lt;&lt; in-&gt;second &lt;&lt; '\n';
    }
}

Reader::~Reader()
{
    write();
}
</code></pre>
<p>EDIT:<br />
Entschuldigung für den Doppelpost, ich dachte, man könne Beiträge löschen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2105013</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2105013</guid><dc:creator><![CDATA[Ki]]></dc:creator><pubDate>Thu, 11 Aug 2011 19:00:19 GMT</pubDate></item></channel></rss>