<?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[daten unbekannter länge einlesen]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>kann mir jemand helfen? Ich möchte eine unbestimmte anzahl integer werte (0 bis 1000) aus einem textfile einlesen.</p>
<p>bei bestimmter länge hab ich das so gemacht:</p>
<pre><code class="language-cpp">#include &quot;Gitter.h&quot;

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;math.h&gt;
#include &lt;vector&gt;

using namespace std;

vector &lt;int&gt; bonds;
vector &lt;double&gt; energy;

Bondzahl::Bondzahl()
{
	ifstream SPIN (&quot;bond_T2.txt&quot;, ios::in);

	if (SPIN.is_open())
	{
		for (int i = 0; i &lt; steps; ++i) 
		{
			SPIN &gt;&gt; bonds[i] &gt;&gt; energy[i];	
			//cout &lt;&lt; bonds[i] &lt;&lt; endl;
		}
		cout &lt;&lt; bonds.size() &lt;&lt; endl;

		SPIN.close();
	}
	else cout &lt;&lt; &quot;Unable to open file&quot;;
}
</code></pre>
<p>Da die Anzahl steps grösser ist, als die Anzahl Integer-Werte im File, werden Nullen drangehängt. Das ist gar nicht gut. Un mit einer while-schleife kann ich das ja auch nicht machen, oder? weil auch nullen im file stehen.</p>
<p>ich könnte in der klasse &quot;Gitter&quot; die integer-werte, die dort in das file eingegeben werden zählen, nur weiss ich nicht, wie ich darauf zugreifen kann.</p>
<p>Kann mir einer einen Tipp geben.</p>
<p>Vielen Dank<br />
Nijntje</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303133/daten-unbekannter-länge-einlesen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 22:04:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303133.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 May 2012 09:13:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:13:10 GMT]]></title><description><![CDATA[<p>Hallo Zusammen,</p>
<p>kann mir jemand helfen? Ich möchte eine unbestimmte anzahl integer werte (0 bis 1000) aus einem textfile einlesen.</p>
<p>bei bestimmter länge hab ich das so gemacht:</p>
<pre><code class="language-cpp">#include &quot;Gitter.h&quot;

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;math.h&gt;
#include &lt;vector&gt;

using namespace std;

vector &lt;int&gt; bonds;
vector &lt;double&gt; energy;

Bondzahl::Bondzahl()
{
	ifstream SPIN (&quot;bond_T2.txt&quot;, ios::in);

	if (SPIN.is_open())
	{
		for (int i = 0; i &lt; steps; ++i) 
		{
			SPIN &gt;&gt; bonds[i] &gt;&gt; energy[i];	
			//cout &lt;&lt; bonds[i] &lt;&lt; endl;
		}
		cout &lt;&lt; bonds.size() &lt;&lt; endl;

		SPIN.close();
	}
	else cout &lt;&lt; &quot;Unable to open file&quot;;
}
</code></pre>
<p>Da die Anzahl steps grösser ist, als die Anzahl Integer-Werte im File, werden Nullen drangehängt. Das ist gar nicht gut. Un mit einer while-schleife kann ich das ja auch nicht machen, oder? weil auch nullen im file stehen.</p>
<p>ich könnte in der klasse &quot;Gitter&quot; die integer-werte, die dort in das file eingegeben werden zählen, nur weiss ich nicht, wie ich darauf zugreifen kann.</p>
<p>Kann mir einer einen Tipp geben.</p>
<p>Vielen Dank<br />
Nijntje</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208705</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208705</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 09:13:10 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:23:32 GMT]]></title><description><![CDATA[<p>Zeig mal den Aufbau der Textfile.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208708</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 09:23:32 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:28:51 GMT]]></title><description><![CDATA[<p>der ist nur so:<br />
1 2<br />
2 3<br />
0 4<br />
3 0<br />
1 1<br />
usw.<br />
mit jeweils einem Tabulator-Schritt dazwischen.<br />
Aber es geht mir primär um die erste Zeile.<br />
Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208711</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 09:28:51 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:39:58 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">for(double first, second; in &gt;&gt; first &gt;&gt; second; v1.push_back(first), v2.push_back(second));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208721</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208721</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sun, 06 May 2012 09:39:58 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:47:00 GMT]]></title><description><![CDATA[<p>Ich nehme an, dass Spalte 1 bonds, und Spalte 2 energy ist. Dann kannst du es z.B. so machen:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;
using namespace std;

int main()
{
	ifstream SPIN (&quot;bond_T2.txt&quot;); // ios::in brauchst du nicht, ist automatisch gesetzt, da ifstream.
	vector&lt;int&gt; bonds;
	vector&lt;double&gt; energy; 

	if(SPIN.is_open() )
	{
		for(int i=0, value=0; SPIN&gt;&gt;value; ++i)
		{
			if( i%2==0 )
			{
				bonds.push_back(value);
			}
			else
			{
				energy.push_back(value);
			}
		}
	}
	else
	{
		cerr &lt;&lt; &quot;could not open file&quot;;
	}
}
</code></pre>
<p>edit: Oder natürlich so wie PI.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208724</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 09:47:00 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:50:31 GMT]]></title><description><![CDATA[<p>Danke euch beiden <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>
<p>super, genau das habe ich gesucht...<br />
grüsse<br />
nijntje</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208725</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 09:50:31 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:52:27 GMT]]></title><description><![CDATA[<p>Edit: Gelöst? Dann ist alles jut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208726</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 09:52:27 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 09:58:51 GMT]]></title><description><![CDATA[<p>Außerdem solltest du dir im Klaren sein, wie die Vorgehensweise beim Lesen ist:</p>
<p>1. Wert aus der Datei lesen.<br />
2. Prüfen, ob das Lesen erfolgreich war.</p>
<pre><code class="language-cpp">for(...; SPIN&gt;&gt;value; ...)... // Wenn du den Lesevorgang in den Bedingungsteil setzt, wird sogleich Schritt 1 als auch Schritt 2 gemacht.
</code></pre>
<p>3. Falls erfolgreich, gelesener Wert verarbeiten.</p>
<pre><code class="language-cpp">bonds.push_back(value);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208730</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 09:58:51 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 10:07:43 GMT]]></title><description><![CDATA[<p>Das war mir nicht ganz klar.<br />
Habe nur C-Programmierung gelernt und bringe mir c++ selbst bei.</p>
<p>Wie ist das dann beim Dateien schreiben?<br />
Ist das so in Ordnung? Es funktioniert zumindest...</p>
<pre><code class="language-cpp">ofstream NRJ (&quot;meanbond.txt&quot;, ios::app|std::ios::out);
NRJ &lt;&lt; mean &lt;&lt;'\t'&lt;&lt; mean_squared &lt;&lt;'\t'&lt;&lt; sigma &lt;&lt;'\t'&lt;&lt; 1./b &lt;&lt;endl;
</code></pre>
<p>nochmals danke fürs Helfen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208734</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 10:07:43 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 10:18:37 GMT]]></title><description><![CDATA[<p>Was dein Beispiel anbelangt, ja - so dürftest du eine Zeile anhängen können. Ein paar Kleinigkeiten:</p>
<pre><code class="language-cpp">ofstream NRJ (&quot;meanbond.txt&quot;, ios::app); //std::out kannst du weglassen
</code></pre>
<pre><code class="language-cpp">1./b
</code></pre>
<p>Der Standard garantiert, dass wenn der Divisor oder der Divident eine Fließkommazahl ist, <a href="http://ideone.com/edqE2" rel="nofollow">alle anderen Typen auch zu dem entsprechendem Typ gecastet werden</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208738</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 10:18:37 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 10:21:36 GMT]]></title><description><![CDATA[<p>Die Vorgehensweise fürs Schreiben ist:</p>
<p>1. Schreiben</p>
<pre><code class="language-cpp">ofstream NRJ (&quot;meanbond.txt&quot;, ios::app|std::ios::out);
NRJ &lt;&lt; mean &lt;&lt;'\t'&lt;&lt; mean_squared &lt;&lt;'\t'&lt;&lt; sigma &lt;&lt;'\t'&lt;&lt; 1./b &lt;&lt;endl;
</code></pre>
<p>2. Prüfen, ob das Schreiben erfolgreich war.</p>
<pre><code class="language-cpp">if( !NRJ ) // identisch mit: if( NRJ.fail() )
{
    // Error: Fehler beim Schreiben.
    // Fehlerbehandlung, das Schreiben z.B. nochmals versuchen.
}
else
{
    // Alles OK, weiter gehts.
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208739</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 10:21:36 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 10:33:39 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>if( !NRJ ) // identisch mit: if( NRJ.fail() )<br />
{</p>
</blockquote>
<p>Nein. Mit <code>if(!NRJ.good())</code> .<br />
Edit: Nein, deins ist <s>auch</s> richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208744</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 10:33:39 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 11:13:28 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<p>Gugelmoser schrieb:</p>
<blockquote>
<p>if( !NRJ ) // identisch mit: if( NRJ.fail() )<br />
{</p>
</blockquote>
<p>Nein. Mit <code>if(!NRJ.good())</code> .<br />
Edit: Nein, deins ist <s>auch</s> richtig.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<blockquote>
<p>bool operator ! ( ) const;<br />
Returns true if either one of the error flags (failbit or badbit) is set on the stream. Otherwise it returns false. This behavior is equivalent to the member function fail().</p>
</blockquote>
<blockquote>
<p>bool fail ( ) const;<br />
The function returns true if either the failbit or the badbit is set.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2208748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208748</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 11:13:28 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 10:33:31 GMT]]></title><description><![CDATA[<p>Gugelmoser schrieb:</p>
<blockquote>
<p>..</p>
</blockquote>
<p>Meins ist sogar falsch. Denn <code>ios::good()</code> gibt zurück ob auch noch das eofbit nicht gesetzt wurde...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208751</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208751</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 10:33:31 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 11:14:12 GMT]]></title><description><![CDATA[<p>Hacker schrieb:</p>
<blockquote>
<p>Gugelmoser schrieb:</p>
<blockquote>
<p>..</p>
</blockquote>
<p>Meins ist sogar falsch. Denn <code>ios::good()</code> gibt zurück ob auch noch das eofbit nicht gesetzt wurde...</p>
</blockquote>
<p>Jop, dann hast du nämlich ein Problem, wenn du nicht zeichenweise ließt:</p>
<pre><code class="language-cpp">int main()
{
	/*
		Dateiinhalt: 123
	*/
	ifstream file(&quot;test.txt&quot;);

	int value;
	file &gt;&gt; value;

	if( !file.good() )
	{
		cerr &lt;&lt; &quot;Fehler beim Lesen&quot;; // Ähh ne, eigentlich nicht.
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208759</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 11:14:12 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 11:42:40 GMT]]></title><description><![CDATA[<p>Ich habe da noch eine Frage:</p>
<p>Wie erhalte ich die Länge dieses Vektors?<br />
Mit v1.size()?</p>
<p>Wenn ich die ersten 10 Zahlen nicht brauche, kann ich dann z.B</p>
<pre><code class="language-cpp">int N0 = 10;
int difference = v1.size() - N0;
</code></pre>
<p>rechnen?<br />
Irgendwie bekomme ich immer ein SIGABRT und weiss nicht wo der Fehler liegt</p>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208782</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 11:42:40 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 11:49:14 GMT]]></title><description><![CDATA[<p>Ich sollte mich klarer ausdrücken</p>
<p>Ich möchte von dem Vektor erst nach gewisser zeit (etwa nach dem 10 wert) den mittelwert ausrechnen, aber nur von jedem 2*tau-ten mal.</p>
<p>so sieht mein code aus:</p>
<pre><code class="language-cpp">vector &lt;int&gt; v1;
vector &lt;double&gt; v2;

Bondzahl::Bondzahl()
{
	ifstream SPIN (&quot;bond_T0.5.txt&quot;);

	if (SPIN.is_open())
	{
		for (int i = 0, value = 0; SPIN &gt;&gt; value; ++i) 
		{
			if (i%2 == 0)
			{
				v1.push_back(value);
			}
			else 
			{
				v2.push_back(value);
			}
		}
		SPIN.close();
	}
	else
	{
		cerr &lt;&lt; &quot;Unable to open file&quot;;
	}

}

int DIFF = v1.size() - N0;

void Bondzahl::Mittelwert(double b)
{
	int tau = 4;   
	int n = DIFF/(2*tau);
	double sum = 0;
	double summe = 0;

	for (int j = N0; j &lt; v1.size(); j+=2*tau) 
	{
		sum += v1[j];
		summe += v1[j]*v1[j];
	}

	//mean value and variance
	//-----------------------

	mean = sum/n;				//mean bondnumber
	mean_squared = summe/n;			//mean bondnumber squared

	sigma = sqrt((1./(double)n)*(mean_squared - mean*mean));	//std

	ofstream NRJ (&quot;meanbond_2mal2.txt&quot;, ios::app|std::ios::out);
	NRJ &lt;&lt; mean &lt;&lt;'\t'&lt;&lt; mean_squared &lt;&lt;'\t'&lt;&lt; sigma &lt;&lt;'\t'&lt;&lt; 1./b &lt;&lt;endl;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208783</guid><dc:creator><![CDATA[nijntje]]></dc:creator><pubDate>Sun, 06 May 2012 11:49:14 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 15:36:57 GMT]]></title><description><![CDATA[<p>Ein weiteres Opfer der eigenen Faulheit. Selber schuld, wenn du globale Variablen benutzt. Lass dir vor Benutzung mal den Wert von DIFF anzeigen und dann versuch mal selbstständig nachzuspüren, warum es diesen Wert hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208837</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 06 May 2012 15:36:57 GMT</pubDate></item><item><title><![CDATA[Reply to daten unbekannter länge einlesen on Sun, 06 May 2012 15:55:45 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Ein weiteres Opfer der eigenen Faulheit.</p>
</blockquote>
<p>Vielleicht hat ihn/sie aber auch einfach ein Wolfs-rudel angegriffen <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="😉"
    /><br />
Er soll sich ein gutes Buch holen. Das hilft am meisten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208847</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 06 May 2012 15:55:45 GMT</pubDate></item></channel></rss>