<?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[Ints und bools Binär lesen&#x2F;schreiben]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich hab ein Problem, nämlich will ich ints und bools binär schreiben und lesen. Wenn ich cast benutze stürzt das Programm ab. Bis jetzt hab ich aber nur Beispiele mit casts gefunden.<br />
Der Code zum Schreiben:</p>
<pre><code class="language-cpp">int MapWrite(char** map, bool** shotable, int x, int y, std::string out)
{
    std::ofstream file(out.c_str(), std::ios::out | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    for(int i=0; i&lt;x; i++)
    {
        for(int a=0; a&lt;y; a++)
        {
            file.write((char*)&amp;map[x][a], sizeof(map[x][a])); //Hier stürzt er ab
            //file.write(reinterpret_cast &lt; char * &gt; (&amp;map[x][y]), sizeof(map[x][y]));
            file.write((char*)&amp;shotable[x][a], sizeof(shotable[x][a]));
            //file.write(reinterpret_cast &lt; char * &gt; (&amp;shotable[x][y]), sizeof(shotable[x][y]));
        }
    }
    return 0;
}
</code></pre>
<p>Der Code zum Lesen, konnte ich noch nicht testen, da ich bis jetzt keine Datei geschrieben bekommen habe:</p>
<pre><code class="language-cpp">int Map::Load(std::string map)
{
    std::string image;
    std::string tmp;
    std::ifstream file(map.c_str(), std::ios::in | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    map_m = new int*[map_x];
    shotable_m = new bool*[map_x];
    for(int x=0; x&lt;map_x; x++)
    {
        map_m[x] = new int[map_y];
        shotable_m[x] = new bool[map_y];
        for(int y=0; y&lt;map_y; y++)
        {
            file.read((char*)&amp;map_m[x][y],sizeof(map_m[x][y])); //int
            file.read((char*)&amp;shotable_m[x][y],sizeof(shotable_m[x][y])); //bool
        }
    }
    file.close();
    return 0;
}
</code></pre>
<p>Danke im voraus.</p>
<p>mfg liggi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223970/ints-und-bools-binär-lesen-schreiben</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 21:42:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223970.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Oct 2008 13:34:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sat, 04 Oct 2008 11:29:48 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich hab ein Problem, nämlich will ich ints und bools binär schreiben und lesen. Wenn ich cast benutze stürzt das Programm ab. Bis jetzt hab ich aber nur Beispiele mit casts gefunden.<br />
Der Code zum Schreiben:</p>
<pre><code class="language-cpp">int MapWrite(char** map, bool** shotable, int x, int y, std::string out)
{
    std::ofstream file(out.c_str(), std::ios::out | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    for(int i=0; i&lt;x; i++)
    {
        for(int a=0; a&lt;y; a++)
        {
            file.write((char*)&amp;map[x][a], sizeof(map[x][a])); //Hier stürzt er ab
            //file.write(reinterpret_cast &lt; char * &gt; (&amp;map[x][y]), sizeof(map[x][y]));
            file.write((char*)&amp;shotable[x][a], sizeof(shotable[x][a]));
            //file.write(reinterpret_cast &lt; char * &gt; (&amp;shotable[x][y]), sizeof(shotable[x][y]));
        }
    }
    return 0;
}
</code></pre>
<p>Der Code zum Lesen, konnte ich noch nicht testen, da ich bis jetzt keine Datei geschrieben bekommen habe:</p>
<pre><code class="language-cpp">int Map::Load(std::string map)
{
    std::string image;
    std::string tmp;
    std::ifstream file(map.c_str(), std::ios::in | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    map_m = new int*[map_x];
    shotable_m = new bool*[map_x];
    for(int x=0; x&lt;map_x; x++)
    {
        map_m[x] = new int[map_y];
        shotable_m[x] = new bool[map_y];
        for(int y=0; y&lt;map_y; y++)
        {
            file.read((char*)&amp;map_m[x][y],sizeof(map_m[x][y])); //int
            file.read((char*)&amp;shotable_m[x][y],sizeof(shotable_m[x][y])); //bool
        }
    }
    file.close();
    return 0;
}
</code></pre>
<p>Danke im voraus.</p>
<p>mfg liggi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1592951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1592951</guid><dc:creator><![CDATA[liggi1998]]></dc:creator><pubDate>Sat, 04 Oct 2008 11:29:48 GMT</pubDate></item><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sun, 05 Oct 2008 12:22:15 GMT]]></title><description><![CDATA[<p>hat keiner eine Idee, warum dass nicht geht?</p>
<p>mfg liggi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593670</guid><dc:creator><![CDATA[liggi1998]]></dc:creator><pubDate>Sun, 05 Oct 2008 12:22:15 GMT</pubDate></item><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sun, 05 Oct 2008 12:42:12 GMT]]></title><description><![CDATA[<p>Zeig doch mal, wie die Variable &quot;map&quot; gefüllt wird. Und der cast bei <code>(char*)&amp;map[x][a]</code> ist erstens unnötig, weil <code>&amp;map[x][a]</code> schon ein <code>char*</code> ist und zweitens ist es ein C-Cast, der in C++ nichts zu suchen hat (siehe static_cast/reinterpret_cast, letzeren sollte man wann immer möglich vermeiden).</p>
<p>edit: Wieso übergibst du bei MapWrite einen char** und keinen int** ? Und: Wieso nimmst du nicht std::vector?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593683</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593683</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Sun, 05 Oct 2008 12:42:12 GMT</pubDate></item><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sun, 05 Oct 2008 16:05:43 GMT]]></title><description><![CDATA[<p>Hier mal die überarbeitete Version vom Schreiben:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;

using namespace std;

int MapWrite(vector&lt;vector&lt;int&gt; &gt; map, vector&lt;vector&lt;bool&gt; &gt; shootable, int x, int y, std::string out)
{
    std::ofstream file(out.c_str(), std::ios::out | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    for(int i=0; i&lt;x; i++)
    {
        for(int a=0; a&lt;y; a++)
        {
            file.write(reinterpret_cast &lt; char * &gt; (&amp;map[x][a]), sizeof(map[x][a])); //Stürzt hier ab
            //file.write(static_cast &lt; char * &gt; (&amp;map[x][a]), sizeof(map[x][a])); //Ergibt Fehlermeldung
            file.write(reinterpret_cast &lt; char * &gt; (&amp;shootable[x][a]), sizeof(shootable[x][a]));
            //file.write(static_cast &lt; char * &gt; (&amp;shootable[x][a]), sizeof(shootable[x][a]));
        }
    }
    return 0;
}

int main()
{
    vector&lt;vector&lt;int&gt; &gt; map;
    vector&lt;vector&lt;bool&gt; &gt; shootable;
    vector&lt;int&gt; temp;
    vector&lt;bool&gt; temp2;
    temp.resize(100, -1);
    temp2.resize(100, true);
    map.resize(100, temp);
    shootable.resize(100, temp2);
    for(int x=0; x&lt;100; x++)
    {
        for(int y=0; y&lt;100; y++)
        {
            if(y&lt;40) map[x][y]=0;
            else map[x][y]=1;
            shootable[x][y]=true;
        }
    }
    return MapWrite(map, shootable, 100, 100, &quot;../map.map&quot;);
}
</code></pre>
<p>Lesen:</p>
<pre><code class="language-cpp">int Map::Load(std::string map)
{
    std::string image;
    std::string tmp;
    std::vector &lt;int&gt; temp;
    std::vector &lt;bool&gt; temp2;
    std::ifstream file(map.c_str(), std::ios::in | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    temp.resize(map_x, -1);
    temp2.resize(map_x, true);
    map_m.resize(map_y, temp);
    shootable_m.resize(map_y, temp2);
    for(int x=0; x&lt;map_x; x++)
    {
        for(int y=0; y&lt;map_y; y++)
        {
            file.read(reinterpret_cast &lt; char * &gt; (&amp;map_m[x][y]),sizeof(map_m[x][y]));
            file.read(reinterpret_cast &lt; char * &gt; (&amp;shootable_m[x][y]),sizeof(shootable_m[x][y]));
        }
    }
    file.close();
    return 0;
}
</code></pre>
<p>mfg liggi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593751</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593751</guid><dc:creator><![CDATA[liggi1998]]></dc:creator><pubDate>Sun, 05 Oct 2008 16:05:43 GMT</pubDate></item><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sun, 05 Oct 2008 17:53:48 GMT]]></title><description><![CDATA[<p>Was sagt denn der Debugger?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593819</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Sun, 05 Oct 2008 17:53:48 GMT</pubDate></item><item><title><![CDATA[Reply to Ints und bools Binär lesen&#x2F;schreiben on Sun, 05 Oct 2008 18:54:52 GMT]]></title><description><![CDATA[<p>ich hab den Fehler beim Schreiben gefunden, beim Zugriff auf den Vector/Array wird die Grenze um eines überschritten.</p>
<p>Der richtige Code:</p>
<pre><code class="language-cpp">int MapWrite(vector&lt;vector&lt;int&gt; &gt; map, vector&lt;vector&lt;bool&gt; &gt; shootable, int x, int y, std::string out)
{
    std::ofstream file(out.c_str(), std::ios::out | std::ios::binary);
    if(!file)
    {
        return -1;
    }
    for(int i=0; i&lt;x; i++)
    {
        for(int a=0; a&lt;y; a++)
        {
            file.write(reinterpret_cast &lt; char * &gt; (&amp;map[i][a]), sizeof(map[i][a]));  //i statt x, x ist z.B. 100 und der größte Index ist 99
            file.write(reinterpret_cast &lt; char * &gt; (&amp;shootable[i][a]), sizeof(shootable[i][a]));
        }
    }
    return 0;
}
</code></pre>
<p>Vielen Dank</p>
<p>mfg liggi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1593844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1593844</guid><dc:creator><![CDATA[liggi1998]]></dc:creator><pubDate>Sun, 05 Oct 2008 18:54:52 GMT</pubDate></item></channel></rss>