<?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[problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt;]]></title><description><![CDATA[<p>Hi, i have the problem mentioned in the header: my file looks as this:</p>
<p>Iso: pfmkk600ta70lp703<br />
T050S 1<br />
D05030 2</p>
<p>Iso: pfmzv600ta70000<br />
T050S 1<br />
D05030 1<br />
R0500200S 9<br />
R0501000S 1</p>
<p>and so on. All terms shall be stored into different containers:Iso.. into a vector,<br />
T050S and so on into a vector&lt; vector&lt;string&gt; &gt; and the numbers like 9 into<br />
vector&lt; vector&lt;int&gt; &gt;.</p>
<p>my code is this:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt; 
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;stdio.h&gt;
using namespace std;

int main() {

  FILE *file;

  vector&lt;string&gt; isometrie;
  vector &lt; vector&lt;int&gt; &gt; isorohranzahl;
  vector &lt; vector&lt;string&gt; &gt; isorohrtypen;
  char tempchar[30];
  vector&lt;string&gt; tempstring;   
  int x;
  int w;                                                                                                                             
   file = fopen(&quot;isometrien.txt&quot;, &quot;r&quot;);
  if(file==NULL) {                                                                                                        
    printf(&quot;Error: can't open file.\n&quot;);
    return 1;
  }
  else {
    printf(&quot;File opened successfully.\n&quot;);
    w = 0 ;
    x=0;
    while(!feof(file)) { 
       isom:;
       fscanf(file, &quot;%s&quot;, tempchar);    //read line
       tempstring.insert(tempstring.end(),tempchar);     //store c string into tempchar
       size_t findiso=tempstring[x].find_last_of(&quot;Iso&quot;);    // insert into stringvector tempstring
       if (findiso!=string::npos)     //if string contains &quot;iso&quot;, create new vector in isorohrtypen and isorohranzahl
       {isometrie.insert(isometrie.end(),tempchar);
      w++; x++;
      isorohrtypen.resize(w);
      isorohranzahl.resize(w);
      goto isom;
      }
       if(findiso=string::npos)
       {x++;
       isorohrtypen[w].insert(isorohrtypen[w].end(),tempchar);     //if &quot;iso&quot; not found, string is a rohr, i.e. insert tempchar into rohrtypen
       isorohranzahl[w].resize(x);
       fscanf(file,&quot;%d&quot;,&amp;isorohranzahl[w][x]);

       }}
// show result
       int g;
     int t;
    for (t=0; t&lt;2; t++)
    {cout&lt;&lt;isometrie[t]&lt;&lt;endl;
        for (g=0; g&lt;2 ;g++)
        {
            cout&lt;&lt;isorohrtypen[t][g]&lt;&lt;&quot; &quot;&lt;&lt;isorohranzahl[t][g]&lt;&lt;endl;

            }

            }}
       fclose(file);
       cin.get();

            }
</code></pre>
<p>i do not get any error messages from the compiler (dev-c++),<br />
but a windows-window pops up and says &quot;there is a problem&quot; and the program doesnt work. could anyone help me, i guess there is a problem with the code <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>
<p>best, sajakob</p>
<p>/edit by pumuckl: removed quote tags inside cpp tags (they are for quoting other people's posts, not code...)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263830/problem-with-reding-a-text-file-into-vector-lt-vector-lt-string-gt-gt</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 13:44:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263830.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Mar 2010 13:10:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 14:34:29 GMT]]></title><description><![CDATA[<p>Hi, i have the problem mentioned in the header: my file looks as this:</p>
<p>Iso: pfmkk600ta70lp703<br />
T050S 1<br />
D05030 2</p>
<p>Iso: pfmzv600ta70000<br />
T050S 1<br />
D05030 1<br />
R0500200S 9<br />
R0501000S 1</p>
<p>and so on. All terms shall be stored into different containers:Iso.. into a vector,<br />
T050S and so on into a vector&lt; vector&lt;string&gt; &gt; and the numbers like 9 into<br />
vector&lt; vector&lt;int&gt; &gt;.</p>
<p>my code is this:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt; 
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;stdio.h&gt;
using namespace std;

int main() {

  FILE *file;

  vector&lt;string&gt; isometrie;
  vector &lt; vector&lt;int&gt; &gt; isorohranzahl;
  vector &lt; vector&lt;string&gt; &gt; isorohrtypen;
  char tempchar[30];
  vector&lt;string&gt; tempstring;   
  int x;
  int w;                                                                                                                             
   file = fopen(&quot;isometrien.txt&quot;, &quot;r&quot;);
  if(file==NULL) {                                                                                                        
    printf(&quot;Error: can't open file.\n&quot;);
    return 1;
  }
  else {
    printf(&quot;File opened successfully.\n&quot;);
    w = 0 ;
    x=0;
    while(!feof(file)) { 
       isom:;
       fscanf(file, &quot;%s&quot;, tempchar);    //read line
       tempstring.insert(tempstring.end(),tempchar);     //store c string into tempchar
       size_t findiso=tempstring[x].find_last_of(&quot;Iso&quot;);    // insert into stringvector tempstring
       if (findiso!=string::npos)     //if string contains &quot;iso&quot;, create new vector in isorohrtypen and isorohranzahl
       {isometrie.insert(isometrie.end(),tempchar);
      w++; x++;
      isorohrtypen.resize(w);
      isorohranzahl.resize(w);
      goto isom;
      }
       if(findiso=string::npos)
       {x++;
       isorohrtypen[w].insert(isorohrtypen[w].end(),tempchar);     //if &quot;iso&quot; not found, string is a rohr, i.e. insert tempchar into rohrtypen
       isorohranzahl[w].resize(x);
       fscanf(file,&quot;%d&quot;,&amp;isorohranzahl[w][x]);

       }}
// show result
       int g;
     int t;
    for (t=0; t&lt;2; t++)
    {cout&lt;&lt;isometrie[t]&lt;&lt;endl;
        for (g=0; g&lt;2 ;g++)
        {
            cout&lt;&lt;isorohrtypen[t][g]&lt;&lt;&quot; &quot;&lt;&lt;isorohranzahl[t][g]&lt;&lt;endl;

            }

            }}
       fclose(file);
       cin.get();

            }
</code></pre>
<p>i do not get any error messages from the compiler (dev-c++),<br />
but a windows-window pops up and says &quot;there is a problem&quot; and the program doesnt work. could anyone help me, i guess there is a problem with the code <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>
<p>best, sajakob</p>
<p>/edit by pumuckl: removed quote tags inside cpp tags (they are for quoting other people's posts, not code...)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874540</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874540</guid><dc:creator><![CDATA[sajakobi]]></dc:creator><pubDate>Sun, 28 Mar 2010 14:34:29 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 14:41:49 GMT]]></title><description><![CDATA[<p>Ich schreib mal auf deutsch weil ich deinen Variablennamen entnehme dass du deutsch kannst. Hier ein paar grundsätzliche Anmerkungen:</p>
<p>- Dev-Cpp ist veraltet. Siehe dazu auch den entsprechenden Thread in der Compiler-und IDE FAQ.<br />
- Mische nicht C++ und C. &lt;stdio.h&gt; ist ein C-Header, in C++ heißt der &lt;cstdio&gt;, allerdings haben die C-Funktionalitäten die du daraus verwendet hast, Nachfolge in C++, so dass du den Header überhaupt nicht brauchst:<br />
[list][<em>]statt FILE benutze fstreams, du hast den passenden Header ja schon eingebaut. [</em>]statt printf benutze cout, auch da hast du den passenden Header schon drin.</p>
<p>Wenn du das umsetzt, sieht dein Code schonmal sehr anders (und auch übersichtlicher) aus und man kann sich auf die Suche nach dem eigentlichen Problem machen.</p>
<p>PS: ich seh grade dass du sowohl printf als auch cout benutzt. Das lässt vermuten, dass du dir den Code einfach irgendwie zusammenkopiert hast. So lernt mans nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874588</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874588</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Sun, 28 Mar 2010 14:41:49 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 17:02:58 GMT]]></title><description><![CDATA[<p>ok vielen dank für die tipps <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="👍"
    /> . ich habe jetzt versucht die dinge zu ändern, welche du bemängelt hast, und noch ein paar kleinigkeiten:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt; 
#include &lt;vector&gt;
#include &lt;sstream&gt;

using namespace std;

int main() {

  vector&lt;string&gt; isometrie;
  vector &lt; vector&lt;int&gt; &gt; isorohranzahl;
  vector &lt; vector&lt;string&gt; &gt; isorohrtypen;
  vector&lt;string&gt; tempstring;
  int x;
  int w;
  char tempchar[20];

  fstream file;
  file.open(&quot;isometrien.txt&quot;, ios::out);

  if(file.is_open()==false) {                                                                                                        
   cout&lt;&lt;&quot;Error: can't open file.\n&quot;;
    return 1;
  }
  else {
    cout&lt;&lt;&quot;File opened successfully.&quot;&lt;&lt;&quot;\n&quot;;
    w = 0 ;
    x=0;
    isom:;
    int p=0;
    while(!file.eof()) { 

       file.getline(tempchar,20);
       tempstring.insert(tempstring.end(),tempchar);
       size_t findiso=tempstring[x].find_last_of(&quot;Iso&quot;);
       if (findiso!=string::npos)
       {isometrie.insert(isometrie.end(),tempstring[x]);
      w++; x++;
      isorohrtypen.resize(w);
      isorohranzahl.resize(w);
      goto isom;
      }
       if(findiso=string::npos)
       {int n=0; p++;
       size_t find_=tempstring[x].find_last_of(&quot; &quot;);
       isorohranzahl[w].resize(p);
       stringstream(tempstring[x].substr(find_+1))&gt;&gt;isorohranzahl[w][p];
       isorohrtypen[w].insert(isorohrtypen[w].end(),tempstring[x].substr(0, find_));
       x++;
       }}
       int g;
     int t;
    for (t=0; t&lt;w; t++)
    {cout&lt;&lt;isometrie[t]&lt;&lt;endl;
        for (g=0; g&lt;(int) isorohranzahl[t].size();g++)
        {
            cout&lt;&lt;isorohrtypen[t][g]&lt;&lt;&quot; &quot;&lt;&lt;isorohranzahl[t][g]&lt;&lt;endl;
            }
            }}
       file.close();
    cin.get();
       }
</code></pre>
<p>problem ist immernoch da: editor gibt &quot;file opened successfully&quot; und windowsfenster poppt hoch und gibt an, dass es ein fehler gegeben hat. Sind weitere grundlegende unstimmigkeiten zu sehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874671</guid><dc:creator><![CDATA[sajakob]]></dc:creator><pubDate>Sun, 28 Mar 2010 17:02:58 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 17:00:29 GMT]]></title><description><![CDATA[<p>Kannst du vielleicht mal kurz erklären was dein Programm machen soll?<br />
Das ist nämlich recht wüst ^^<br />
Ich denke du willst irgendwie aus dem Textfile auslesen welche Rohre in welcher Stückzahl vorhanden sind. Da würde sich eigentlich ein wenig OOP anbieten.<br />
<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="😉"
    /> eine Klasse Rohr, die ISO-Nummer speichert und Infos gibt über Länge, Durchmesser, Wanddicke, usw.<br />
<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="😉"
    /> Eine Klasse RohrLager, die für jeden Rohrtyp eine Anzahl speichert.<br />
oder in der Art.</p>
<p>Denn getrennte Listen, die irgendwie dann über Indizes gemappt werden sind nicht recht schön...</p>
<p>(Ich bin aber jetzt nicht tiefer in deine Programmlogik eingestiegen, ist immerhin Sonntag ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874679</guid><dc:creator><![CDATA[l&#x27;abra d&#x27;or]]></dc:creator><pubDate>Sun, 28 Mar 2010 17:00:29 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 17:28:45 GMT]]></title><description><![CDATA[<p>genau: es gibt sogenannte isometrien (gewisse zusammenstellungen von rohren) diese sollen in einem vector gespeichert werden, und es gibt die dazugehörigen rohre mit ihrer anzahl welche jeweils in &quot;matrizen&quot; gespeichert werden sollten.</p>
<p>es muss im nachhinein auf all diese parameter zurückgegriffen werden können, und die anzahl der elemente in den containern sollte variabel bleiben, deshalb dachte ich vectoren seien das richtige.</p>
<p>vielen dank, ich schaue mal nach OOP... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874687</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874687</guid><dc:creator><![CDATA[sajakob]]></dc:creator><pubDate>Sun, 28 Mar 2010 17:28:45 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 18:54:30 GMT]]></title><description><![CDATA[<p>sajakob schrieb:</p>
<blockquote>
<p>... ich schaue mal nach OOP... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
</blockquote>
<p>Ja, das ist eine gute Idee! Wenn man einen Datensatz als Klasse oder Struktur modelliert, tut man sich leichter. Das Einlesen eines Datensatzes ist ein bisschen tricky, da das Ende eines Datensatzes nur durch den Anfang des nächsten gebildet wird.</p>
<p>So geht's:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;limits&gt;

// --   ein Datensatz als struct modelliert
struct IsoWeisNichWas // bitte selber einen passenden Namen geben
{
    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, IsoWeisNichWas&amp; x );

    struct RohrEintrag
    {
        std::string m_typ;
        int m_anzahl;
    };
    std::string m_isometrie;
    std::vector&lt; RohrEintrag &gt; m_rohrEintraege;
};

std::istream&amp; operator&gt;&gt;( std::istream&amp; in, IsoWeisNichWas&amp; x )
{
    using namespace std;
    if( in &gt;&gt; x.m_isometrie )
    {
        vector&lt; IsoWeisNichWas::RohrEintrag &gt; rohrEintraege;
        // Bem.: hier wird ausgenutzt, dass das erste Wort eines Datensatzes 'Iso:' ist.
        //  aus dem selben Grund muss das Fileende berücksicht werden, damit der Stream beim letzten Datensatz nicht in den Zustand fail rutscht.
        for( IsoWeisNichWas::RohrEintrag r; !in.eof() &amp;&amp; !(in &gt;&gt; ws).eof() &amp;&amp; in &gt;&gt; r.m_typ &amp;&amp; r.m_typ != &quot;Iso:&quot; &amp;&amp; in &gt;&gt; r.m_anzahl; )
            rohrEintraege.push_back( r ); // alles ok, dann Rohreintrag speichern
        swap( rohrEintraege, x.m_rohrEintraege ); // eingelesen Rohrtypen übernehmen
    }
    return in;
}

int main() 
{
    using namespace std;
    vector&lt; IsoWeisNichWas &gt; ergebnis;
    ifstream file( &quot;isometrien.txt&quot; );
    file.ignore( numeric_limits&lt; streamsize &gt;::max(), ':' ); // erstes &quot;Iso:&quot; überlesen
    for( IsoWeisNichWas x; file &gt;&gt; x; ) // bis Fehler oder End-Of-File
        ergebnis.push_back( x );
    if( file.eof() )
    {
        // alles iO.; Lesen ist bei End-Of-File angekommen
        cout &lt;&lt; ergebnis.size() &lt;&lt; &quot; Datensaetze gelesen&quot; &lt;&lt; endl;
        // das Ergebnis steht jetzt in 'ergebnis'
        // ...
    }

    cin.get();
}
</code></pre>
<p>Schau Dir auch <a href="http://www.c-plusplus.net/forum/viewtopic-var-p-is-1874662.html#1874662" rel="nofollow">dieses Posting</a> an, das hilft beim Verstehen.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874735</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 28 Mar 2010 18:54:30 GMT</pubDate></item><item><title><![CDATA[Reply to problem with reding a text file into vector&amp;lt; vector&amp;lt;string&amp;gt; &amp;gt; on Sun, 28 Mar 2010 19:37:19 GMT]]></title><description><![CDATA[<p>besten dank 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="👍"
    /> , ich brauche ein bisschen um zum verstehen, aber gut das spornt an</p>
<p>trotzdem finde ich in meinem weniger eleganten versuch den fehler nicht. ABER wenn ich zeilen 50-52 (in dem als 2. aufgeführten code) ignoriere kommt keine fehlermeldung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874748</guid><dc:creator><![CDATA[sajakob]]></dc:creator><pubDate>Sun, 28 Mar 2010 19:37:19 GMT</pubDate></item></channel></rss>