<?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[von fstream in structs]]></title><description><![CDATA[<p>Hi</p>
<p>Wie kann ich aus einer Datei auslesen und in eine struct schreiben?</p>
<pre><code class="language-cpp">struct Weapon
    {
        unsigned int id;
        unsigned short type;
        unsigned short level;
        unsigned short strength;
        unsigned short speed;
        unsigned short weight;
        unsigned short range;
        unsigned int price;
        std::string name;
    };
</code></pre>
<p>File:</p>
<pre><code># ID Type Level Name Strength Speed Weight Price Range
# Swords        
1011 0 1 Sword 2 3 2 200 0
1021 0 2 Longsword 3 4 2.3 700 0
1031 0 3 Claymore 5 6 2.5 1200 0
# Axes        
1111 1 1 Axe 3 2 2.5 200 0
1121 1 2 Poleaxe 4 3 2.8 700 0
1131 1 3 Battleaxe 6 5 3.2 1200 0
</code></pre>
<p>Hab schon getline gefunden, aber dann ist ja alles in einem string, wie kann ich das &quot;wortweise&quot;, also jeweils bis zum Leerzeichen zerpflücken?</p>
<p>Danke im voraus <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/topic/175957/von-fstream-in-structs</link><generator>RSS for Node</generator><lastBuildDate>Sat, 19 Sep 2026 10:49:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/175957.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Mar 2007 19:09:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:09:54 GMT]]></title><description><![CDATA[<p>Hi</p>
<p>Wie kann ich aus einer Datei auslesen und in eine struct schreiben?</p>
<pre><code class="language-cpp">struct Weapon
    {
        unsigned int id;
        unsigned short type;
        unsigned short level;
        unsigned short strength;
        unsigned short speed;
        unsigned short weight;
        unsigned short range;
        unsigned int price;
        std::string name;
    };
</code></pre>
<p>File:</p>
<pre><code># ID Type Level Name Strength Speed Weight Price Range
# Swords        
1011 0 1 Sword 2 3 2 200 0
1021 0 2 Longsword 3 4 2.3 700 0
1031 0 3 Claymore 5 6 2.5 1200 0
# Axes        
1111 1 1 Axe 3 2 2.5 200 0
1121 1 2 Poleaxe 4 3 2.8 700 0
1131 1 3 Battleaxe 6 5 3.2 1200 0
</code></pre>
<p>Hab schon getline gefunden, aber dann ist ja alles in einem string, wie kann ich das &quot;wortweise&quot;, also jeweils bis zum Leerzeichen zerpflücken?</p>
<p>Danke im voraus <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/1246278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246278</guid><dc:creator><![CDATA[Eldoran]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:09:54 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:16:16 GMT]]></title><description><![CDATA[<p>Oder besser gefragt,<br />
warum geht</p>
<pre><code class="language-cpp">void test_streams()
{
        ifstream items( &quot;items&quot; );
        if( items.is_open() )
        {
            cout &lt;&lt; &quot;File open&quot; &lt;&lt; endl;
            while( items.good() )
            {
                cout &lt;&lt; &quot;File good&quot; &lt;&lt; endl;
                stringstream sstr;
                items.get( *sstr.rdbuf() );
                std::cout &lt;&lt; sstr &lt;&lt; std::endl;
            }
        }
}
</code></pre>
<p>nicht?<br />
Gibt mir nur pointer Addressen aus.. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246284</guid><dc:creator><![CDATA[Eldoran]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:16:16 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:23:32 GMT]]></title><description><![CDATA[<p>nimm doch einfach den &gt;&gt; operator ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246288</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246288</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:23:32 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:29:48 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<p>nimm doch einfach den &gt;&gt; operator ...</p>
</blockquote>
<p>Hm, tja so einfach gehts..<br />
Aber wie kann ich eine Zeile überspringen bei '#' ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246292</guid><dc:creator><![CDATA[Eldoran]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:29:48 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:37:31 GMT]]></title><description><![CDATA[<p>lies einfach mit get ein zeichen am zeilenanfang, und wenns kein # ist, tus mit put zurück in den stream, ansonsten kannste die Zeile mit getline überspringen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246297</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246297</guid><dc:creator><![CDATA[phlox81]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:37:31 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 19:47:01 GMT]]></title><description><![CDATA[<p>Eldoran schrieb:</p>
<blockquote>
<p>Hab schon getline gefunden, aber dann ist ja alles in einem string, wie kann ich das &quot;wortweise&quot;, also jeweils bis zum Leerzeichen zerpflücken?</p>
</blockquote>
<p>getline ist nur dann sinnvoll, wenn das Zeilenende zur Formatierung der Daten notwendig ist. Das ist aber bei Dir so gar nicht der Fall.</p>
<p>Eldoran schrieb:</p>
<blockquote>
<p>(D)Evil schrieb:</p>
<blockquote>
<p>nimm doch einfach den &gt;&gt; operator ...</p>
</blockquote>
<p>Hm, tja so einfach gehts..<br />
Aber wie kann ich eine Zeile überspringen bei '#' ?</p>
</blockquote>
<p>Genau; der operator&gt;&gt; tut genau das was notwendig ist. Und für das Überspringen der Kommentare sorgt ein sogenannter Manipulator. Alles zusammen sieht so aus:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;cmath&gt;        // std::floor für korrektes runden
#include &lt;iterator&gt;     // istream_iterator

// --   Kommentar überlesen
std::istream&amp; comment( std::istream&amp; in )
{
    typedef std::istream::traits_type traits_type;
    in &gt;&gt; std::ws;  // ggf. Leerzeichen überlesen
    for( int m; !traits_type::eq_int_type( m = in.peek(), traits_type::eof() )
        &amp;&amp; traits_type::to_char_type( m ) == '#'; )
    {
        in.ignore( 9999, traits_type::to_int_type( '\n' ) );
    }
    return in;
}

struct Weapon
{
    unsigned int id;
    unsigned short type;
    unsigned short level;
    unsigned short strength;
    unsigned short speed;
    unsigned short weight;
    unsigned short range;
    unsigned int price;
    std::string name;

    // --   Weapon einlesen
    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Weapon&amp; w_ )
    {
        // # ID Type Level Name Strength Speed Weight/10 Price Range
        Weapon w;
        double weight_;
        if( in &gt;&gt; comment &gt;&gt; w.id &gt;&gt; w.type &gt;&gt; w.level &gt;&gt; w.name
            &gt;&gt; w.strength &gt;&gt; w.speed &gt;&gt; weight_ &gt;&gt; w.price &gt;&gt; w.range )
        {
            w.weight = std::floor( weight_ * 10.0 + 0.5 );
            w_ = w;
        }
        return in;
    }
};

int main()
{ 
    using namespace std;
    ifstream items( &quot;items&quot; );
    vector&lt; Weapon &gt; weapons( (istream_iterator&lt; Weapon &gt;( items )), istream_iterator&lt; Weapon &gt;() );
    // .. das war's
    return 0;
}
</code></pre>
<p>Bem.: ich habe mal angenommen, dass das Gewicht mit Faktor 10 in der struct Weapon abgelegt ist.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246302</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246302</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 15 Mar 2007 19:47:01 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 20:05:42 GMT]]></title><description><![CDATA[<p>Ui <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /><br />
Danke!<br />
Werd das mal durchgehen, ob ich's kapier.</p>
<blockquote>
<p>Bem.: ich habe mal angenommen, dass das Gewicht mit Faktor 10 in der struct Weapon abgelegt ist.</p>
</blockquote>
<p>Das hab ich vorhin auch gemerkt. Wird dann wohl eher ein double, als ein short werden.</p>
<p>Aber mir stellt sich schon die nöchste Frage.<br />
Mein File sieht im Moment so aus:</p>
<pre><code># Weapons        
# ID Type Level Name Strength Speed Weight Price Range
# Swords        
1011 0 1 Sword 2 3 2 200 0
1021 0 2 Longsword 3 4 2.3 700 0
1031 0 3 Claymore 5 6 2.5 1200 0
# Axes        
1111 1 1 Axe 3 2 2.5 200 0
1121 1 2 Poleaxe 4 3 2.8 700 0
1131 1 3 Battleaxe 6 5 3.2 1200 0
# Armour        
# ID Type Level Name Absorb Handicap Weight Price 
# Helms        
2011 0 1 Lether 3 2 1.6 500 
2021 0 2 Chain 5 3 2.4 1000 
2031 0 3 Massive 8 4 3.2 1500 
# Cuirass        
2111 1 1 Lether 3 2 0.9 700 
2121 1 2 Chain 5 3 1.2 1400 
2131 1 3 Massive 8 4 1.8 2100
</code></pre>
<p>Und es kommen wohl noch weitere Arten von Items dazu.<br />
Die Waffen und Rüstungen sollen aber in verschiedene sets, da ich keine polymorphen Objekte machen will. Hab ich da ne Chance das auf deine elegante Art zu machen? Oder soll ich lieber verschiedene Files machen?</p>
<p>Danke nochmal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246311</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246311</guid><dc:creator><![CDATA[Eldoran]]></dc:creator><pubDate>Thu, 15 Mar 2007 20:05:42 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Thu, 15 Mar 2007 21:42:18 GMT]]></title><description><![CDATA[<p>Eldoran schrieb:</p>
<blockquote>
<p>Hab ich da ne Chance das auf deine elegante Art zu machen?</p>
</blockquote>
<p>im Prinzip schon. Die Frage ist zunächst mal wo in der Datei die Waffen aufhören und die Rüstungen anfangen. Mal angenommen es sind immer genau 6, dann ist es einfach. Du erstellst eine struct Armour nebst operator&gt;&gt; nach dem gleichen Muster wie Weapon und im Hauptprogramm liest Du 6 Waffen und 6 Rüstungen</p>
<pre><code class="language-cpp">int main()
{ 
    using namespace std;
    vector&lt; Weapon &gt; weapons;
    vector&lt; Armour &gt; armours;
    ifstream items( &quot;items&quot; );
    Weapon w;
    for( int n = 0; n&lt;6 &amp;&amp; items &gt;&gt; w; ++n )
        weapons.push_back( w );

    // usw. für die Rüstungen
    return 0;
}
</code></pre>
<p>Falls Du eine dynamische Anzahl haben möchtest, kannst Du in der Datei entweder die Anzahl davor schreiben (einfach und sicher) oder Dir eine Markierung ausdenken, die das Ende der Waffen angibt und dabei natürlich von einem Kommentar und einer Zahl eindeutig zu unterscheiden ist. Letzteres ist nur zu empfehlen, wenn die Datei von Hand editiert werden soll, weil dann die Angabe der Anzahl redundant und daher fehleranfällig ist. Das Einlesen ist dann aber etwas schwieriger zu programmieren.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1246366</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1246366</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 15 Mar 2007 21:42:18 GMT</pubDate></item><item><title><![CDATA[Reply to von fstream in structs on Sat, 17 Mar 2007 16:44:38 GMT]]></title><description><![CDATA[<p>So, habs nun hinbekommen, dynamisch mit Steuerzeichen.<br />
Wohl nicht sehr elegant, aber es tut was es soll.</p>
<pre><code class="language-cpp">typedef std::istream::traits_type traits_type;

// überspringe Kommentarzeilen &quot;#&quot;
    std::istream&amp; skip_comment( std::istream&amp; in )
    {
        in &gt;&gt; std::ws;
        for( traits_type::int_type m; 
            !traits_type::eq_int_type( m = in.peek(), traits_type::eof() )
             &amp;&amp; traits_type::to_char_type( m ) == '#'; )
        {
            in.ignore( 9999, traits_type::to_int_type( '\n' ) );
        }
        return in;
    }

// überspringe Zeilen mit Steuerungsbefehl &quot;!&quot;
    std::istream&amp; skip_command( std::istream&amp; in )
    {
        in &gt;&gt; std::ws;
        for( traits_type::int_type m; 
            !traits_type::eq_int_type( m = in.peek(), traits_type::eof() )
             &amp;&amp; traits_type::to_char_type( m ) == '!'; )
        {
            in.ignore( 9999, traits_type::to_int_type( '\n' ) );
        }
        return in;
    }

// lade Werte aus Datei
    void BattleSystem::load_values()
    {
        enum Section
        {
            NONE = 0,
            WEAPON = 1,
            ARMOR = 2
        };

        Section section = NONE;
        ifstream items( &quot;items&quot; );
        if( items.is_open() )
        {
            while( items.good() )
            {
                if( '!' == traits_type::to_char_type( items.peek() ) )
                {
                    section = static_cast&lt;Section&gt;(section + 1);
                    items &gt;&gt; skip_command;    // Nach Bearbeitung des Befehls Position weiterschieben
                }
                switch( section )
                {
                    case WEAPON:
                    {
                        Weapon weapon;
                        items &gt;&gt; weapon;    // Entsprechende operator&gt;&gt; überladung s.o.
                        m_weapons.insert( weapon );
                        break;
                    }
                    case ARMOR:
                    {
                        Armor armor;
                        items &gt;&gt; armor;    // dito
                        m_armors.insert( armor );
                        break;
                    }
                    default:
                        break;
                }
                items.ignore();    // ohne dies lädt er nur die Waffen :confused:
            }
        }
        items.close();
    }
</code></pre>
<p>Und das File sieht so aus:</p>
<pre><code>!
#Weapons
# ID Type Level Name Strength Speed Weight Price Range
# Swords        
1011 0 1 Sword 2 3 2 200 0
1021 0 2 Longsword 3 4 2.3 700 0
1031 0 3 Claymore 5 6 2.5 1200 0
# Axes        
1111 1 1 Axe 3 2 2.5 200 0
1121 1 2 Poleaxe 4 3 2.8 700 0
1131 1 3 Battleaxe 6 5 3.2 1200 0
!
#Armor#
# ID Type Level Name Absorb Handicap Weight Price 
# Helms        
2011 0 1 Lether 3 2 1.6 500 
2021 0 2 Chain 5 3 2.4 1000 
2031 0 3 Massive 8 4 3.2 1500 
# Cuirass        
2111 1 1 Lether 3 2 0.9 700 
2121 1 2 Chain 5 3 1.2 1400 
2131 1 3 Massive 8 4 1.8 2100
! #EOF
</code></pre>
<p>Danke nochmal vielmals euch allen, vor allem Werner <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
Gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1247357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1247357</guid><dc:creator><![CDATA[Eldoran]]></dc:creator><pubDate>Sat, 17 Mar 2007 16:44:38 GMT</pubDate></item></channel></rss>