<?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 bei Stringstream und Einlesung]]></title><description><![CDATA[<p>Hallo,<br />
es geht darum, dass ich Datenblöcke habe, die so aufgebaut sind:</p>
<pre><code>usemtl HAND
f 1 2 3
f 2 3 4
f 3 4 5
usemtl KOPF
f 3 2 1
f 6 5 4
f 9 8 7
f 1 2 3
f 2 3 4
f 3 4 5
</code></pre>
<p>Es kommt also ein &quot;usemtl [Objectname]&quot; vor und darauf folgen verschiedene Anzahlen von Indices (Faces). Der bisherige Parser (Code im unteren Abschnitt) soll nun durch das File gehen, checken ob ein &quot;usemtl&quot; vorkommt und ab da bis zum nächsten usemtl alle Faces einlesen. Und joa, dass war's im Prinzip schon, dass will noch nicht so ganz funktionieren. Wichtig ist halt nur, dasss wirklich bis zum nächsten &quot;usemtl&quot; alle Faces eingelesen werden und der komplette Datenblock in den Vector zurückgepusht werden wird. Wie dem auch sei, hier der aktuelle Code:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt; 
#include &lt;math.h&gt; 
#include &lt;stdio.h&gt; 
#include &lt;stdarg.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;string.h&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

class Vector3
{
public:
	Vector3();
	~Vector3();

	float x;
	float y;
	float z;
};

struct Face
{
	Face()
	{
		vertexIndex[0] = vertexIndex[1] = vertexIndex[2] = 0xfffffff;
	}
	std::string mtlName;
	std::string vertexIndex[3];
};

std::string const getWord(std::string Line, int Pos) 
{
	std::vector&lt;std::string&gt; Vec;
	std::stringstream ss(Line);
	std::string Word, a;
    while(ss &gt;&gt; Word)
    Vec.push_back(Word);

	return (Pos+1 &gt; Vec.size() ? &quot;&quot; : Vec[Pos] );
}

int main(void)
{
	std::ifstream File;
	std::string Line;
	unsigned int numFaces=0;
	std::vector&lt;Face&gt; FaceBlock;

	File.open(&quot;3005.obj&quot;,std::ios::in);

	if(File.fail()) std::cout&lt;&lt;&quot;File loading failed!&quot;;

	while(!File.eof())
	{
		getline(File, Line);

		std::string const l = getWord(Line,0);

		if(l==&quot;usemtl&quot;) std::cout&lt;&lt;&quot;New Material!&quot;&lt;&lt;std::endl;
		{
			Face face;
			face.mtlName = getWord(Line,1);

			while(!File.eof())
			{
				getline(File, Line);
				/*std::vector&lt;float&gt; x;
				 std::vector&lt;float&gt; y;
				 std::vector&lt;float&gt; z;*/
				//std::vector&lt;Vector3&gt; FaceCoords;
				std::string const nl = getWord(Line,0);

				if(nl==&quot;f&quot;)
				{
					/*FaceCoords.x = convertToFloat(getWord(Line,1));
					  FaceCoords.y = convertToFloat(getWord(Line,2));
					  FaceCoords.z = convertToFloat = convertToFloat(getWord(Line,3));*/

					/*x = getWord(Line,1);
				      y = getWord(Line,2);
					  z = getWord(Line,3);*/

					Face face;

					face.vertexIndex[0] = getWord(Line,1); //&lt;- x
					face.vertexIndex[1] = getWord(Line,2); //&lt;- y
					face.vertexIndex[2] = getWord(Line,3); //&lt;- z

					face;

					++numFaces;
					for(int i=0; i &lt; 3; i++)
					{
						std::cout&lt;&lt;face.vertexIndex[i];
					}

				if(nl==&quot;usemtl&quot;) std::cout&lt;&lt;&quot;New material-actual     Facenumber:&quot;&lt;&lt; numFaces &lt;&lt;std::endl;
				{
					FaceBlock.push_back(face);
					std::cout&lt;&lt;FaceBlock[0].mtlName;
					break;
				}
				}
			}

		}
	}

	//std::cout&lt;&lt;&quot;Total number of Faces:&quot;&lt;&lt; numFaces;
	getch();
}
</code></pre>
<p>Kann den Code leider nicht bearbeiten, also bitte erbarmen wegen den Einrückungen. Das Problem ist nun, dass er mir nur jeden zweiten Face-Index ausliesst und ein neues &quot;newmtl&quot; scheint er auch nicht zu finden.<br />
Einer ne Idee?</p>
<p>ich 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/233749/problem-bei-stringstream-und-einlesung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 17:01:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/233749.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Feb 2009 18:19:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem bei Stringstream und Einlesung on Sun, 08 Feb 2009 18:20:06 GMT]]></title><description><![CDATA[<p>Hallo,<br />
es geht darum, dass ich Datenblöcke habe, die so aufgebaut sind:</p>
<pre><code>usemtl HAND
f 1 2 3
f 2 3 4
f 3 4 5
usemtl KOPF
f 3 2 1
f 6 5 4
f 9 8 7
f 1 2 3
f 2 3 4
f 3 4 5
</code></pre>
<p>Es kommt also ein &quot;usemtl [Objectname]&quot; vor und darauf folgen verschiedene Anzahlen von Indices (Faces). Der bisherige Parser (Code im unteren Abschnitt) soll nun durch das File gehen, checken ob ein &quot;usemtl&quot; vorkommt und ab da bis zum nächsten usemtl alle Faces einlesen. Und joa, dass war's im Prinzip schon, dass will noch nicht so ganz funktionieren. Wichtig ist halt nur, dasss wirklich bis zum nächsten &quot;usemtl&quot; alle Faces eingelesen werden und der komplette Datenblock in den Vector zurückgepusht werden wird. Wie dem auch sei, hier der aktuelle Code:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt; 
#include &lt;math.h&gt; 
#include &lt;stdio.h&gt; 
#include &lt;stdarg.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;string.h&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

class Vector3
{
public:
	Vector3();
	~Vector3();

	float x;
	float y;
	float z;
};

struct Face
{
	Face()
	{
		vertexIndex[0] = vertexIndex[1] = vertexIndex[2] = 0xfffffff;
	}
	std::string mtlName;
	std::string vertexIndex[3];
};

std::string const getWord(std::string Line, int Pos) 
{
	std::vector&lt;std::string&gt; Vec;
	std::stringstream ss(Line);
	std::string Word, a;
    while(ss &gt;&gt; Word)
    Vec.push_back(Word);

	return (Pos+1 &gt; Vec.size() ? &quot;&quot; : Vec[Pos] );
}

int main(void)
{
	std::ifstream File;
	std::string Line;
	unsigned int numFaces=0;
	std::vector&lt;Face&gt; FaceBlock;

	File.open(&quot;3005.obj&quot;,std::ios::in);

	if(File.fail()) std::cout&lt;&lt;&quot;File loading failed!&quot;;

	while(!File.eof())
	{
		getline(File, Line);

		std::string const l = getWord(Line,0);

		if(l==&quot;usemtl&quot;) std::cout&lt;&lt;&quot;New Material!&quot;&lt;&lt;std::endl;
		{
			Face face;
			face.mtlName = getWord(Line,1);

			while(!File.eof())
			{
				getline(File, Line);
				/*std::vector&lt;float&gt; x;
				 std::vector&lt;float&gt; y;
				 std::vector&lt;float&gt; z;*/
				//std::vector&lt;Vector3&gt; FaceCoords;
				std::string const nl = getWord(Line,0);

				if(nl==&quot;f&quot;)
				{
					/*FaceCoords.x = convertToFloat(getWord(Line,1));
					  FaceCoords.y = convertToFloat(getWord(Line,2));
					  FaceCoords.z = convertToFloat = convertToFloat(getWord(Line,3));*/

					/*x = getWord(Line,1);
				      y = getWord(Line,2);
					  z = getWord(Line,3);*/

					Face face;

					face.vertexIndex[0] = getWord(Line,1); //&lt;- x
					face.vertexIndex[1] = getWord(Line,2); //&lt;- y
					face.vertexIndex[2] = getWord(Line,3); //&lt;- z

					face;

					++numFaces;
					for(int i=0; i &lt; 3; i++)
					{
						std::cout&lt;&lt;face.vertexIndex[i];
					}

				if(nl==&quot;usemtl&quot;) std::cout&lt;&lt;&quot;New material-actual     Facenumber:&quot;&lt;&lt; numFaces &lt;&lt;std::endl;
				{
					FaceBlock.push_back(face);
					std::cout&lt;&lt;FaceBlock[0].mtlName;
					break;
				}
				}
			}

		}
	}

	//std::cout&lt;&lt;&quot;Total number of Faces:&quot;&lt;&lt; numFaces;
	getch();
}
</code></pre>
<p>Kann den Code leider nicht bearbeiten, also bitte erbarmen wegen den Einrückungen. Das Problem ist nun, dass er mir nur jeden zweiten Face-Index ausliesst und ein neues &quot;newmtl&quot; scheint er auch nicht zu finden.<br />
Einer ne Idee?</p>
<p>ich 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/1660106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660106</guid><dc:creator><![CDATA[Kóyaánasqatsi]]></dc:creator><pubDate>Sun, 08 Feb 2009 18:20:06 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Stringstream und Einlesung on Mon, 09 Feb 2009 06:57:44 GMT]]></title><description><![CDATA[<p>Hey Leute übertreibt nicht mit den Antworten^^.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660304</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660304</guid><dc:creator><![CDATA[Kóyaánasqatsi]]></dc:creator><pubDate>Mon, 09 Feb 2009 06:57:44 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Stringstream und Einlesung on Mon, 09 Feb 2009 07:02:48 GMT]]></title><description><![CDATA[<p>Mal eine andere Frage, ist es möglich die Datei in einem anderem Format aufzubauen?Sowas wie CSV Format, ich denke das macht die Geschichte des Einlesens etwas einfacher oder lieg ich da falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660307</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660307</guid><dc:creator><![CDATA[Firefighter]]></dc:creator><pubDate>Mon, 09 Feb 2009 07:02:48 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Stringstream und Einlesung on Mon, 09 Feb 2009 07:15:13 GMT]]></title><description><![CDATA[<p>Hey,<br />
nein, leider nicht. Die Datei steht schon in einer Datei (OBJ). Trotzdem danke für den Hinweis.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660316</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660316</guid><dc:creator><![CDATA[Kóyaánasqatsi]]></dc:creator><pubDate>Mon, 09 Feb 2009 07:15:13 GMT</pubDate></item><item><title><![CDATA[Reply to Problem bei Stringstream und Einlesung on Mon, 09 Feb 2009 11:04:57 GMT]]></title><description><![CDATA[<p>Wenn man einen Block mit 'usemtl', dem Namen und den folgeden Faces als 'Faceblock' bezeichnet, dann ist in der Datei mehr als ein Faceblock abgelegt. Mindestens das sollte sich in Deinem Programm wiederfinden lassen.<br />
Als Strukturen sind also notwendig:</p>
<pre><code class="language-cpp">struct Face
{
    Face()
    {
        vertexIndex[0] = vertexIndex[1] = vertexIndex[2] = 0;
    }
    friend std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Face&amp; f )
    {
        return in &gt;&gt; f.vertexIndex[0] &gt;&gt; f.vertexIndex[1] &gt;&gt; f.vertexIndex[2];
    }

    int vertexIndex[3];
};

struct FaceBlock
{
    FaceBlock()
        : mtlName()
        , m_faces()
    {}
    std::string mtlName;
    std::vector&lt; Face &gt; m_faces;
};
</code></pre>
<p>Face habe ich gleich mal mit einer Einlesefunktion versehen.</p>
<p>Daneben wäre dann ein Container (vector) von Faceblocks notwendig, der den Inhalt aufnimmt. Alles zusammen kann dann so aussehen:</p>
<pre><code class="language-cpp">std::vector&lt; FaceBlock &gt; faceBlocks; 
    std::ifstream File(&quot;3005.obj&quot;);
    FaceBlock fb;
    bool first = true;
    for( std::string wort; File &gt;&gt; wort; )
    {
        if( wort == &quot;usemtl&quot; )
        {
            if( !first )
            {
                faceBlocks.push_back( fb );
                fb.m_faces.clear();
            }
            first = false;
            File &gt;&gt; fb.mtlName;                
        }
        else if( wort == &quot;f&quot; )
        {
            // if( first ) // evt. prüfen, ob vorher auch ein 'usemtl' gelesen wurde
            //     File.setstate( std::ios_base::failbit );
            Face face;
            if( File &gt;&gt; face )
                fb.m_faces.push_back( face );
        }
        else // ggf. weitere Token auswerten (dann zuletzt gelesenen Faceblock nicht vergessen!)
            File.setstate( std::ios_base::failbit ); // ansonsten ist das ein Fehler
    }
    if( !first &amp;&amp; File.eof() )
        faceBlocks.push_back( fb ); // letzten Faceblock übernehmen
    else
        std::cerr &lt;&lt; &quot;Fehler beim Lesen &quot; &lt;&lt; std::endl;
</code></pre>
<p>Die Lösung ist erstmal 'straight forward' - sie taugt nicht unbedingt dazu, ein <a href="http://www.royriggs.com/obj.html" rel="nofollow">OBJ-File in voller Schönheit</a> einzulesen. Aber ich wollte es hier nicht gleich übertreiben.</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660390</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660390</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Mon, 09 Feb 2009 11:04:57 GMT</pubDate></item></channel></rss>