<?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[texfile in einen vector]]></title><description><![CDATA[<p>Hallo Jungs,</p>
<p>mal ne ganz bescheidene Frage.</p>
<p>Wie kriege am schnellsten ein textfile in ein vector&lt;vector&lt;const char*&gt; &gt; rein.</p>
<p>Ist der Zugriff auf ein vector schneller als auf ein texfile????</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/264098/texfile-in-einen-vector</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 03:59:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/264098.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 01 Apr 2010 21:56:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to texfile in einen vector on Thu, 01 Apr 2010 21:56:44 GMT]]></title><description><![CDATA[<p>Hallo Jungs,</p>
<p>mal ne ganz bescheidene Frage.</p>
<p>Wie kriege am schnellsten ein textfile in ein vector&lt;vector&lt;const char*&gt; &gt; rein.</p>
<p>Ist der Zugriff auf ein vector schneller als auf ein texfile????</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876645</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876645</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Thu, 01 Apr 2010 21:56:44 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Thu, 01 Apr 2010 22:20:19 GMT]]></title><description><![CDATA[<p>ismaild schrieb:</p>
<blockquote>
<p>Wie kriege am schnellsten ein textfile in ein vector&lt;vector&lt;const char*&gt; &gt; rein.</p>
</blockquote>
<p>Gar nicht. Wenn <code>const char *</code> eine Zeile sein soll, ergibt der doppelte <code>vector</code> keinen Sinn.</p>
<p>ismaild schrieb:</p>
<blockquote>
<p>Ist der Zugriff auf ein vector schneller als auf ein texfile????</p>
</blockquote>
<p>Ja, RAM ist mindestens 1000 mal schneller als eine Festplatte (darum geht es hier wohl).</p>
<p>Um eine Textdatei zu öffnen kannst du <code>std::ifstream</code> verwenden. Es gibt verschiedene Möglichkeiten, den Inhalt zu lesen (siehe Google oder so).<br />
Geeignete Datenstrukturen für den Inhalt wären <code>std::vector&lt;std::string&gt;</code> für ein Array der Zeilen oder einfach <code>std::string</code> für den Inhalt als Ganzes.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876651</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 01 Apr 2010 22:20:19 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Thu, 01 Apr 2010 22:30:20 GMT]]></title><description><![CDATA[<p>Besten Dank eigentlich wollte ich die Datei, welche aus mehreren Spalten in jeder Zeile besteht(delimited) in ein 2 dimensionales raster ziehen, sodas ich nachher mittels zwei koordinaten an jedem Punkt(zeile/spalte) landen kann...</p>
<p>Deswegen auch der doppelte vector ;o)</p>
<p>Also ich rede hier von texfiles welche 5 mio ZEilen oder mehr haben. Ergo wollte ich keine 5mio x spalten stringobjekte erstellen.(Denke ich verkehrt ???)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876655</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Thu, 01 Apr 2010 22:30:20 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Thu, 01 Apr 2010 23:57:38 GMT]]></title><description><![CDATA[<p>Also Freunde ist ja net so das ich nix mache täte hier mal ein Beispiel:</p>
<pre><code>int main(int argc, char* argv[])
{                                                                                       
    const char* fname = &quot;3mio.txt&quot;;
    FILE *fp = fopen( fname, &quot;rb&quot; );

std::ofstream datei(&quot;cout.txt&quot;);

vector&lt;vector &lt;char* &gt; &gt; vecFile;
vector &lt;char* &gt; vecCol;

int i(0);

        char buff[BUFSIZ];
        char* test=0;
        char token[100];token[0]='\0';
         int x = setvbuf(fp, (char *)NULL, _IOLBF, BUFSIZ*10);
         while ( fgets( buff, sizeof buff, fp ) != NULL ) {

                char * ptrCR=strchr(buff,'\r');
                *ptrCR = '\0';

                int zu(0);
                while(buff[zu] != '\0')
                {
                    char aktZeichen[2];aktZeichen[1]='\0';
                    aktZeichen[0] = buff[zu];
                    if(buff[zu] != ';')
                    {
                        strcat(token,aktZeichen);
                    }
                    else{
                        vecCol.push_back(_strdup(token));
                        token[0]='\0';
                    }

                    zu++;
                }
                vecFile.push_back(vecCol);
                vecCol.clear();
         }
        _getch();// speicher bei ca. 1 gb

        for(int iZeile(0); iZeile &lt; vecFile.size();iZeile++)
                datei &lt;&lt; vecFile[iZeile][9] &lt;&lt; endl;
</code></pre>
<p>So in etwa hatte ich mir das gedacht. Da müsste doch was zu optimieren sein oder???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876670</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Thu, 01 Apr 2010 23:57:38 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 00:34:29 GMT]]></title><description><![CDATA[<p>ismaild schrieb:</p>
<blockquote>
<p>So in etwa hatte ich mir das gedacht. Da müsste doch was zu optimieren sein oder???</p>
</blockquote>
<p>Möglicherweise ist es schneller, die ganze Datei mit <code>fread</code> oder <code>ifstream::read</code> in den Speicher zu holen und dann zu parsen. Auf jeden Fall kann man den <code>vector</code> optimieren. Durch das <code>push_back</code> muss <code>vecFile</code> immer wieder vergrößert werden. Schneller wird es, wenn zu Anfang mit <code>vector::reserve</code> annähernd genug Speicher für alle Zeilen angefordert wird. Falls du weißt, wie viele Spalten die Datei hat, lässt sich der innere <code>vector</code> durch etwas wie <code>boost::array&lt;char *, ANZAHL_SPALTEN&gt;</code> ersetzen, um teure <code>new[]</code> s zu sparen. Wenn sich die Länge der einzelnen Einträge in Grenzen hält, könnte man <code>_strdup</code> rausnehmen und den <code>char*</code> durch <code>array&lt;char, MAX_LENGTH&gt;</code> ersetzen.<br />
Wenn man das alles berücksichtigt, benötigt das Programm nur ein einziges <code>new[]</code> am Anfang.</p>
<p>Um Arbeitsspeicher zu sparen, könnte man die Datei blockweise lesen und die Ergebnisse sofort in die Ausgabedatei schreiben. Wahrscheinlich wird das sowieso nötig sein, weil Windows nicht unbedingt Speicheranforderungen von 1 GB oder mehr zulässt. Ich vermute auch, dass diese Variante sogar schneller wäre, weil <code>vecFile</code> wesentlich kleiner sein müsste, da es wiederverwendet werden könnte ( <code>vector::clear</code> gibt in der Regel noch nicht den Speicher frei).</p>
<p>Ach ja, mit der Ausgabe könnte man es ähnlich machen und diese puffern, bevor sie binär in die Datei geschrieben wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876679</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Fri, 02 Apr 2010 00:34:29 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 18:31:54 GMT]]></title><description><![CDATA[<p>Besten Dank für Deine wertvollen Ideen. Also dass mit dem fread habe ich net ganz verstanden. MEinst Du die komplette Datei auf einmal reinzuholen und dann parsen. Wie geht das denn... Leider weis ich net immer von vorne herein wieviele Zeilen oder spalten ich habe, aber die Zahl der Spalten pro Datei ist immer gleich...</p>
<p>Leider muss ich die Datei erst im Speicher halten um gewisse operationen durchführen zu können....</p>
<p>HAst Du mal ein Beispiel für den boost::array damit habe ich ja granix am Hut gehabt bis jetzt..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876966</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Fri, 02 Apr 2010 18:31:54 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 20:17:03 GMT]]></title><description><![CDATA[<p>So habe mal die ganze sache mit boost gemacht hier der code:</p>
<pre><code>using namespace std;
using namespace boost;

int main(int argc, char* argv[])
{
    const char* fname = &quot;5mio.txt&quot;;
    FILE *fp = fopen( fname, &quot;rb&quot; );

std::ofstream datei(&quot;cout.txt&quot;);

vector&lt;boost::array&lt;char*,10&gt; &gt; vecFile;
boost::array &lt;char*,10&gt; vecCol;

int i(0);
        char buff[BUFSIZ];
        char* test=0;
        char token[70];token[0]='\0';
         int x = setvbuf(fp, (char *)NULL, _IOLBF, BUFSIZ*10);
         while ( fgets( buff, sizeof buff, fp ) != NULL ) {

                char * ptrCR=strchr(buff,'\r');
                *ptrCR = '\0';
                int zu(0);
                while(buff[zu] != '\0')
                {
                    char aktZeichen[2];aktZeichen[1]='\0';
                    aktZeichen[0] = buff[zu];
                    if(buff[zu] != ';')
                    {
                        strcat(token,aktZeichen);
                    }
                    else{
                        vecCol.assign(_strdup(token));
                        token[0]='\0';
                    }
                    zu++;
                }
                vecFile.push_back(vecCol);
                vecCol.empty();
         }
        _getch();// ram bei 830mb

        vector&lt;boost::array &lt;char*,10 &gt; &gt;::iterator curr(vecFile.begin());
        vector&lt;boost::array &lt;char*,10 &gt; &gt;::iterator end(vecFile.end());
        for( ; curr &lt; end; ++curr)
        {
        datei &lt;&lt; (*curr)[9]&lt;&lt; &quot;\n&quot;;
        }

    return 0;
}
</code></pre>
<p>Kann man das array nicht dynamisch allozieren muss man des vorher bestimmen im zweiten Argument...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876990</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Fri, 02 Apr 2010 20:17:03 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 20:21:09 GMT]]></title><description><![CDATA[<p>Du machst das denke ich viel zu kompliziert.. Zeig nochmal genau, was du für ein File hast und wie das genau haben willst.</p>
<p>Hier einfach mal boost zu benutzen macht das ganze nicht besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876996</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 02 Apr 2010 20:21:09 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 20:43:07 GMT]]></title><description><![CDATA[<p>Also habe ein simples textfile:</p>
<p>feld1(varchar);feld2(varchar);feld3(varchar);......feldn(varchar)</p>
<p>und das ganze mal 5mio zeilen oder 3mio also immer ziemlich gross..<br />
und ich experimentiere gerade wie ich das ganze sehr schnell und speichersparend in ein 2d raster(zeile/spalte) bekomme.. das ist alles</p>
<p>P.S.: besten dank fürs einklinken....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877002</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Fri, 02 Apr 2010 20:43:07 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 20:49:22 GMT]]></title><description><![CDATA[<p>Und wie genau willst du das in mehrere Dimensionen unterteilen?<br />
Und welche Daten von dem File willst du auch speichern?</p>
<p>Mach mal ein kleines Beispiel mit 2-3 Einträgen, wie ein Inputfile aussieht und wie du das dann gespeichert haben willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877003</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 02 Apr 2010 20:49:22 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Fri, 02 Apr 2010 21:11:48 GMT]]></title><description><![CDATA[<p>Also:</p>
<p>feld1;feld2;feld3<br />
hans;mercedes;new york<br />
evelyn;trabant;moskau</p>
<p>Und jetzt will ich das ganze in ein quasi zwei dimensionales array packen</p>
<p>ich will jede zeile und spalte mit einem index ansprechen können...</p>
<p>klar genug</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877010</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877010</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Fri, 02 Apr 2010 21:11:48 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Sat, 03 Apr 2010 06:34:52 GMT]]></title><description><![CDATA[<p>ismaild schrieb:</p>
<blockquote>
<p>feld1(varchar);feld2(varchar);feld3(varchar);......feldn(varchar)</p>
</blockquote>
<p>Keinerlei Infos über Redundanz, Inhalt, Größe?</p>
<p>ismaild schrieb:</p>
<blockquote>
<p>sehr schnell und speichersparend</p>
</blockquote>
<p>Speichersparend bei beliebigem Text wird bei 1char pro Zeichen sein ... wenn du keine wilden Sonderzeichen drin hast. Du könntest versuchen längere Textpassagen mit Pack-Algos zu verkleinern (bz2?) was aber Rechenzeit zu en/dekodieren verbraucht also dem &quot;sehr schnell&quot; entgegenspricht.</p>
<p>Wirklich platzsparend wird das nur gehen wenn in den 5 mio Zeilen * n spalten<br />
der selbe Text immer wieder kommt ... dann könntest du eine Qmap &lt; QString, int&gt; nehmen (jedem Text eine ID zuweisen) und dann die Ints speichern ... wäre kürzer sofern der meiste Text &gt;&gt; 4 characters hat ...</p>
<p>Bei der Dateigröße ist mE allerdings ein sequentielles Auslesen besser ...</p>
<p>5 mio Zeilen, 4 chars pro spalte, 10 values pro Zeile<br />
= 5.000.000 * 4 * 10<br />
= 200.000.000 chars (ohne seperatoren) / 1024 / 1024<br />
= 190 MB Daten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877059</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Sat, 03 Apr 2010 06:34:52 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Sat, 03 Apr 2010 21:36:56 GMT]]></title><description><![CDATA[<p>So hab mal bischen modifiziert.</p>
<pre><code>int main(int argc, char* argv[])
{
    const char *delims = &quot;;&quot;;
    const char* fname = &quot;3mio.txt&quot;;
    FILE *fp = fopen( fname, &quot;rb&quot; );

    fseek(fp, 0, SEEK_END);
    long pos = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *bytes = (char *)malloc(pos);
    fread(bytes, pos, 1, fp);

    fseek(fp, 0, SEEK_SET);
    int lines(0),cols(0);
    int i(0);
    for (; i&lt;=pos; i++)
    {
        if(bytes[i] == *delims &amp;&amp; lines &lt; 1)
            cols++;
        if(bytes[i] == '\n')
            lines++;
    }
    free(bytes);
    cols++;

    typedef boost::multi_array&lt;char*, 2&gt; array_type;
    typedef array_type::index index;
    array_type meineDaten(boost::extents[lines][cols]);
    std::ofstream datei(&quot;cout.txt&quot;);

    char buff[BUFSIZ]; 
    int x = setvbuf(fp, (char *)NULL, _IOLBF, BUFSIZ*10);
    int imyline(0),imycol(0);
    size_t start = 0;char token[50];

    while ( fgets( buff, sizeof buff, fp ) != NULL )
    {
        while (buff[start] != '\0')
        {
            size_t end = strcspn(buff + start, delims);
            strncpy(token, buff + start, end);token[end]= '\0';
            meineDaten[imyline][imycol]=_strdup(token);
            start += (buff[start + end] != '\0') ? end + 1 : end;
            imycol++;
        }
        start = 0;
        imyline++;
        imycol=0;
    }
    //_getch();
    for(index i = 0; i != lines; ++i)
        datei &lt;&lt; meineDaten[i][9] &lt;&lt; &quot;\n&quot;;
    return 0;
}
</code></pre>
<p>Und was meint Ihr...??</p>
<p>Hat einer einen Vorschlag wie ich in der inneren while-schleife das stringcopy direkt in mein array bekomme sodass ich _strdup() mir sparen könnte..</p>
<p>Für jede verbesserug bin ich offen.<br />
Achja habs diesmal mit dem boost gemacht...<br />
der Speicher bewegt sich bei ca. 800mb aber ist ziemlich schnell..</p>
<p>P.S.: wusstet Ihr das es schneller ist ein &lt;&lt;&quot;\n&quot;; anzuhängen als &lt;&lt;endl; zu benutzen.....nur mal so in den Raum geschmissen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877320</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Sat, 03 Apr 2010 21:36:56 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Sun, 04 Apr 2010 18:00:22 GMT]]></title><description><![CDATA[<p>liegt denke ich daran, dass endl nicht nur \n ist sondern auch flush, also den Schreibbuffer in die Datei zwangsentleert statt dann, wenn das OS dazu hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877572</guid><dc:creator><![CDATA[Münzbieger]]></dc:creator><pubDate>Sun, 04 Apr 2010 18:00:22 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Sun, 04 Apr 2010 23:09:18 GMT]]></title><description><![CDATA[<p>Ih versuche gerade mal einen natives array zu benutzen, aber irgendwie ist es net so intuitiv wir der von boost.</p>
<p>Also habe ein</p>
<pre><code>char* array_2D[lines][cols];
</code></pre>
<p>wie muss ich jetzt dem array mein token übergeben....??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877666</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877666</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Sun, 04 Apr 2010 23:09:18 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Mon, 05 Apr 2010 12:29:56 GMT]]></title><description><![CDATA[<p>So habe es jetzt fertig...</p>
<pre><code>int main(int argc, char* argv[])
{
    unsigned int startTime = 0;
    unsigned int endTime = 0;

    const char *delims = &quot;;&quot;;
    const char* fname = &quot;5mio.txt&quot;;
    FILE *fp = fopen( fname, &quot;rb&quot; );

    fseek(fp, 0, SEEK_END);
    long pos = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *bytes = (char *)malloc(pos);
    fread(bytes, pos, 1, fp);

    fseek(fp, 0, SEEK_SET);
    int lines(0),cols(0);
    int i(0);
    for (; i&lt;=pos; i++)
    {
        if(lines &lt; 1 &amp;&amp; bytes[i] == *delims)
            cols++;
        if(bytes[i] == '\n')
            lines++;
    }
    free(bytes);
    cols++;

    // Create the native array
    char*** myArray=allocate_2d_array_of_chars(lines,cols);
    std::ofstream datei(&quot;cout.txt&quot;);

    char buff[BUFSIZ];
    int x = setvbuf(fp, (char *)NULL, _IOLBF, BUFSIZ*10);
    int imyline(0),imycol(0);
    while ( fgets( buff, sizeof buff, fp ) != NULL )
    {
        while (buff[start] != '\0')
        {
            size_t end = strcspn(buff + start, delims);
            char token[end];
            char szString[end];
            szString[end]='\0';
            strncpy(token, buff + start, end);
            token[end]='\0';
            if(imycol== (cols-1))
            {
                int i2 = 0;
                for (int i = 0; i &lt; end; i++)
                {
                    char c = token[i];
                    if (c != '\r' &amp;&amp; c != '\n')
                    {
                        szString[i2++] = c;
                        szString[i2]='\0';
                    }
                }
                myArray[imyline][imycol]=_strdup(szString);

            }
            else
            {
                myArray[imyline][imycol]=_strdup(token);

            }
            start += (buff[start + end] != '\0') ? end + 1 : end;
            imycol++;
        }
        start = 0;
        imyline++;
        imycol=0;
    }

    for(int i = 0; i &lt; lines; ++i)
    {
        for(int j = 0; j &lt; cols; ++j)
        {
            (j==0)?(datei &lt;&lt; myArray[i][j]):(datei &lt;&lt; delims &lt;&lt; myArray[i][j]);
        }
        datei &lt;&lt; &quot;\n&quot;;
    }

    return 0;
}

char*** allocate_2d_array_of_chars(size_t rows, size_t columns)
{
    int i;
    char ***db_array;

    db_array = (char ***)malloc ( rows * sizeof *db_array);

    if ( db_array == NULL )
    {
        puts (&quot;Speicherreservierung nicht möglich&quot;);
        return NULL;
    }

    for ( i = 0; i&lt;rows; i++)
    {
        db_array[i] = (char **)malloc ( columns * sizeof *db_array[i]);
        if ( db_array[i] == NULL )
            printf (&quot;Speicherreservierung bei db_array[%d] nicht möglich\n&quot;, i);
    }

    return db_array;
}
</code></pre>
<p>Hab da mal ne Frage...Wenn ich die Datei einlese habe ich am Ende einer jeden Zeile ein \r\n.. soweit ja kein Problem, aber wenn ich selber versuche diese beim schreiben in die Datei wieder einzusetzen bekomme ich noch eine Leerzeile zwischen den Zeilen....</p>
<p>Und noch was.. die Datei die ich einlese hat ca. 300mb, wenn die Datei im Array ist habe ich einen Seicherverbrauch bei ca. 900mb, also man müsste meinen die Datei wird redundant gepeichert oder so...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877793</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877793</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Mon, 05 Apr 2010 12:29:56 GMT</pubDate></item><item><title><![CDATA[Reply to texfile in einen vector on Mon, 05 Apr 2010 12:41:06 GMT]]></title><description><![CDATA[<p>Habe nochmal versucht mit memory mapped files die Sache etwas zu beschleunigen.</p>
<p>hier der Code:</p>
<pre><code>namespace io = boost::iostreams;
int main(int argc, char* argv[])
{
    unsigned int startTime = 0;
    unsigned int endTime = 0;

    const char *delims = &quot;;&quot;;
    const char* fname = &quot;5mio.txt&quot;;
    FILE *fp = fopen( fname, &quot;rb&quot; );

    fseek(fp, 0, SEEK_END);
    long pos = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *bytes = (char *)malloc(pos);
    fread(bytes, pos, 1, fp);

    fseek(fp, 0, SEEK_SET);
    int lines(0),cols(0);
    int i(0);
    for (; i&lt;=pos; i++)
    {
        if(lines &lt; 1 &amp;&amp; bytes[i] == *delims)
            cols++;
        if(bytes[i] == '\n')
            lines++;
    }
    free(bytes);
    cols++;

    io::mapped_file_source myMap(&quot;5mio.txt&quot;);
    io::stream&lt;io::mapped_file_source&gt; input(myMap);
    // Create the native array
    char*** myArray=allocate_2d_array_of_chars(lines,cols);
    std::ofstream datei(&quot;cout.txt&quot;);
    std::string line;

    char buff[BUFSIZ];

    int imyline(0),imycol(0);

    while(std::getline(input, line))
    {
        strcpy(buff,line.c_str());
        while (buff[start] != '\0')
        {
            size_t end = strcspn(buff + start, delims);
            char token[end];
            char szString[end];
            szString[end]='\0';
            strncpy(token, buff + start, end);
            token[end]='\0';
            if(imycol== (cols-1))
            {
                int i2 = 0;
                for (int i = 0; i &lt; end; i++)
                {
                    char c = token[i];
                    if (c != '\r' &amp;&amp; c != '\n')
                    {
                        szString[i2++] = c;
                        szString[i2]='\0';
                    }
                }
                myArray[imyline][imycol]=_strdup(szString);
            }
            else
            {
                myArray[imyline][imycol]=_strdup(token);
            }
            start += (buff[start + end] != '\0') ? end + 1 : end;
            imycol++;
        }
        start = 0;
        imyline++;
        imycol=0;
    }

    for(int i = 0; i &lt; lines; ++i)
    {
        for(int j = 0; j &lt; cols; ++j)
        {
            (j==0)?(datei &lt;&lt; myArray[i][j]):(datei &lt;&lt; delims &lt;&lt; myArray[i][j]);
        }
        datei &lt;&lt; &quot;\n&quot;;
    }

    return 0;
}

char*** allocate_2d_array_of_chars(size_t rows, size_t columns)
{
    int i;
    char ***db_array;

    db_array = (char ***)malloc ( rows * sizeof *db_array);

    if ( db_array == NULL )
    {
        puts (&quot;Speicherreservierung nicht möglich&quot;);
        return NULL;
    }

    for ( i = 0; i&lt;rows; i++)
    {
        db_array[i] = (char **)malloc ( columns * sizeof *db_array[i]);
        if ( db_array[i] == NULL )
            printf (&quot;Speicherreservierung bei db_array[%d] nicht möglich\n&quot;, i);
    }

    return db_array;
}
</code></pre>
<p>Die Sache ist das Dingen mit Mapping ist langsamer, ca. 3sek.<br />
komisch oder....</p>
<p>P.S.: Bin für jede Verbesserung offen und dankbar...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877795</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877795</guid><dc:creator><![CDATA[ismaild]]></dc:creator><pubDate>Mon, 05 Apr 2010 12:41:06 GMT</pubDate></item></channel></rss>