<?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[Tabelle aus Datei lesen]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich muß eine Datei die Messwerte enthält konvertieren. Die Datei ist ungefähr so aufgebaut:<br />
a b c<br />
0 -0.00004 -1.44433 -0.00003<br />
1 -0.00003 -1.22222 -0.00044<br />
usw</p>
<p>In der neuen Datei sollen die Spalten die Zeilen sein.</p>
<p>Jetzt will ich erstmal die einzelnen Werte in Variablen zum Weiterarbeiten speichern.</p>
<p>Beim auslesen hab ich aber schon das Problem, daß stringstream mir nur eine Zahl nach dem &quot;Komma&quot; mitgibt.<br />
Hier mal mein Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
using namespace std;

int main()
{  
  ifstream fin(&quot;rein&quot;,ios::binary );
  ofstream fout(&quot;raus&quot;,ios::binary );
  string str;
  int j=0,i=0;
  double v[10];
  cout &lt;&lt; &quot;Hier der Inhalt der Datei:\n&quot;;
  while (fin.good()) {
   getline(fin,str,'\n');
   cout &lt;&lt;str&lt;&lt;endl;
   stringstream s;
   s&lt;&lt;str;
   s&gt;&gt;v[i++]&gt;&gt;v[i++];
  }
  cout&lt;&lt; &quot;\nEnde der Ausgabe\n&quot;;

  cout&lt;&lt;&quot;Variableninhalt:\n&quot;;
  for(j=0;j&lt;i;j++)
  {
    cout&lt;&lt;v[j]&lt;&lt;endl;                 
  }
  fin.close();
  fout.close();
  cin.get();
}
</code></pre>
<p>Ich bin leider mit C++ nicht sehr bewandert und such mir das Zeug im Netz zusammen.Try&amp;Error eben.<br />
Also wenn einer 'ne Idee hätte...</p>
<p>Danke!<br />
LG<br />
Stefan</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/189005/tabelle-aus-datei-lesen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 06:43:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/189005.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 07 Aug 2007 11:51:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Tue, 07 Aug 2007 11:51:52 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich muß eine Datei die Messwerte enthält konvertieren. Die Datei ist ungefähr so aufgebaut:<br />
a b c<br />
0 -0.00004 -1.44433 -0.00003<br />
1 -0.00003 -1.22222 -0.00044<br />
usw</p>
<p>In der neuen Datei sollen die Spalten die Zeilen sein.</p>
<p>Jetzt will ich erstmal die einzelnen Werte in Variablen zum Weiterarbeiten speichern.</p>
<p>Beim auslesen hab ich aber schon das Problem, daß stringstream mir nur eine Zahl nach dem &quot;Komma&quot; mitgibt.<br />
Hier mal mein Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
using namespace std;

int main()
{  
  ifstream fin(&quot;rein&quot;,ios::binary );
  ofstream fout(&quot;raus&quot;,ios::binary );
  string str;
  int j=0,i=0;
  double v[10];
  cout &lt;&lt; &quot;Hier der Inhalt der Datei:\n&quot;;
  while (fin.good()) {
   getline(fin,str,'\n');
   cout &lt;&lt;str&lt;&lt;endl;
   stringstream s;
   s&lt;&lt;str;
   s&gt;&gt;v[i++]&gt;&gt;v[i++];
  }
  cout&lt;&lt; &quot;\nEnde der Ausgabe\n&quot;;

  cout&lt;&lt;&quot;Variableninhalt:\n&quot;;
  for(j=0;j&lt;i;j++)
  {
    cout&lt;&lt;v[j]&lt;&lt;endl;                 
  }
  fin.close();
  fout.close();
  cin.get();
}
</code></pre>
<p>Ich bin leider mit C++ nicht sehr bewandert und such mir das Zeug im Netz zusammen.Try&amp;Error eben.<br />
Also wenn einer 'ne Idee hätte...</p>
<p>Danke!<br />
LG<br />
Stefan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340106</guid><dc:creator><![CDATA[postmann]]></dc:creator><pubDate>Tue, 07 Aug 2007 11:51:52 GMT</pubDate></item><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Tue, 07 Aug 2007 14:20:38 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;

int main()
{  
	std::ifstream file_in(&quot;data.dat&quot;, std::ios::in);
	if (!file_in) std::cout &lt;&lt; &quot;Couldn't find file!&quot; &lt;&lt; std::endl; return 0;

	std::cout &lt;&lt; &quot;Dateiinhalt:\n &lt;&lt; =======&lt;&gt;======= &gt;&gt; &quot; &lt;&lt; std::endl;
	std::stringstream ss;
	ss &lt;&lt; file_in.rdbuf();
	std::string file_content = ss.str();
	std::cout &lt;&lt; file_content &lt;&lt; &quot;\n &lt;&lt; =======&lt;&gt;======= &gt;&gt; &quot; &lt;&lt; std::endl;
}
</code></pre>
<p>so hasse schonmal den Dateinhalt ... sollte so gehen ^^ hab es nicht kontrolliert ...</p>
<p>hmm aber in deinem Fall wäre es so besser:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;

int main()
{  

	std::ifstream file_in(&quot;data.dat&quot;, std::ios::in);
	if (!file_in) std::cout &lt;&lt; &quot;Couldn't find file!&quot; &lt;&lt; std::endl; return 0;

	std::cout &lt;&lt; &quot;Dateiinhalt:\n &lt;&lt; =======&lt;&gt;======= &gt;&gt; &quot; &lt;&lt; std::endl;
	std::vector&lt;std::vector&lt;std::string&gt;&gt; data;
	std::string file_line;
	while (std::getline(file_in, file_line))
	{
		std::cout &lt;&lt; file_line &lt;&lt; std::endl;
		std::vector&lt;std::string&gt; cur_line_data;
		for (std::size_t pos = file_line.find_first_of(' '); !file_line.empty(); pos = file_line.find_first_of(' '))
		{
			cur_line_data.push_back(file_line.substr(0, pos));
			file_line.erase(0, pos);
		}
		data.push_back(cur_line_data);
	}
	std::cout &lt;&lt; &quot; &lt;&lt; =======&lt;&gt;======= &gt;&gt; &quot; &lt;&lt; std::endl;
}
</code></pre>
<p>... data[0][1] wäre bsw. &quot;0&quot; ... und data[0][2] dann &quot;-0.00004&quot; ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340239</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340239</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Tue, 07 Aug 2007 14:20:38 GMT</pubDate></item><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Tue, 07 Aug 2007 15:34:14 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if (!file_in) std::cout &lt;&lt; &quot;Couldn't find file!&quot; &lt;&lt; std::endl; return 0;
</code></pre>
</blockquote>
<p>Hallo (D)Evil,<br />
ich hatte Dein Programm ausprobiert und schon an mir verzweifelt bevor ich den fatalen Fehler in dieser Zeile gefunden habe!</p>
<p>Hallo Stefan,<br />
wenn ich Deine Anforderung richtig verstanden habe, so sollte folgendes Programm ein Lösungsansatz sein:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;iomanip&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;

struct Entry
{
    int m_id;
    double m_abc[3];
};
std::istream&amp; operator&gt;&gt;( std::istream&amp; in, Entry&amp; e )
{
    return in &gt;&gt; e.m_id &gt;&gt; e.m_abc[0] &gt;&gt; e.m_abc[1] &gt;&gt; e.m_abc[2];
}

int main()
{
    using namespace std;
    ifstream fin( &quot;rein&quot; );
    if( !fin.is_open() )
    {
        cerr &lt;&lt; &quot;Fehler beim Oeffnen der Datei&quot; &lt;&lt; endl;
        return -1;
    }
    string kopf[3];
    fin &gt;&gt; kopf[0] &gt;&gt; kopf[1] &gt;&gt; kopf[2];  // a b c lesen
    vector&lt; Entry &gt; buf( (istream_iterator&lt; Entry &gt;( fin )), istream_iterator&lt; Entry &gt;() );
    cout &lt;&lt; buf.size() &lt;&lt; &quot; Eintraege gelesen&quot; &lt;&lt; endl;

    ofstream fout( &quot;raus&quot; );
    // vier Zeilen schreiben
    const int spalte1_breite = 4;
    const int spalteN_breite = 9;
    // 1. Zeile
    fout &lt;&lt; setw(spalte1_breite) &lt;&lt; &quot; &quot;;    // 1.Spalte leer
    for( vector&lt; Entry &gt;::iterator i = buf.begin(); i != buf.end(); ++i )
        fout &lt;&lt; setw(spalteN_breite) &lt;&lt; i-&gt;m_id;
    fout &lt;&lt; endl;

    fout &lt;&lt; fixed &lt;&lt; setprecision( spalteN_breite - 4 ); 
    // Anzahl Nachkommastellen: breite - (1xZwischenraum + 1xVorzeichen + 1xDezimalpunkt + 1xVorkommastelle)
    // 2.- 4. Zeile
    for( int idx = 0; idx &lt; 3; ++idx )
    {
        fout &lt;&lt; setw(spalte1_breite) &lt;&lt; kopf[idx];    // 1.Spalte
        for( vector&lt; Entry &gt;::iterator i = buf.begin(); i != buf.end(); ++i )
            fout &lt;&lt; setw(spalteN_breite) &lt;&lt; i-&gt;m_abc[idx];
        fout &lt;&lt; endl;
    }
    return 0;
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340294</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 07 Aug 2007 15:34:14 GMT</pubDate></item><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Tue, 07 Aug 2007 15:39:21 GMT]]></title><description><![CDATA[<blockquote>
<p>Hallo (D)Evil,<br />
ich hatte Dein Programm ausprobiert und schon an mir verzweifelt bevor ich den fatalen Fehler in dieser Zeile gefunden habe!</p>
</blockquote>
<p>Hmm ... hasst de recht ... war mir nicht sicher ob das nicht doch ging ... steht ja in einer Zeile <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="😉"
    /></p>
<p>Ähm ich war nicht davon ausgegangen das immer genau 3 Inhalte drin stehen und das abc jeweils überschriften sind sowohl 012 usw. die Zeilennummern ... dann kannst du es so machen wie Werner meinte ... dynamische Anzahl von Spalten und Zeilen usw. hasse aber bei meinem Ansatz <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340298</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340298</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Tue, 07 Aug 2007 15:39:21 GMT</pubDate></item><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Wed, 08 Aug 2007 08:12:15 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>erstmal vielen Dank für eure Hilfe. Werd im Moment anderswo gebraucht (Praktikant halt)und somit liegt das Prog wieder auf Eis.<br />
Ich verstehe bei den Programmen leider einiges nicht, werd sie aber als Anregung betrachten und mich anhand den Schlagwörtern einlesen.<br />
Wir hatten leider in den 2 C/C++Semester einen Prof, bei dem wir nicht wirklich was lernten. Ich kann jetzt mit Schleifen, Fallunterscheidungen, Klassen usw. umgehen, aber z.b. using namespacestd, std::, strings, ifstream, arbeiten mit Dateien usw sind mir fremd.<br />
Werd wohl noch ein Selbststudium dranhängen <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="😉"
    /></p>
<p>LG<br />
Stefan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340564</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340564</guid><dc:creator><![CDATA[postmann]]></dc:creator><pubDate>Wed, 08 Aug 2007 08:12:15 GMT</pubDate></item><item><title><![CDATA[Reply to Tabelle aus Datei lesen on Wed, 08 Aug 2007 13:35:37 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">using namespace std;
</code></pre>
<p>Wenn de das schreibst, musst du kein std:: mehr vor die Funktionen schreiben ... untergräbst aber auch den Sinn von Namespaces ... da du so bsw. eine Funktion ausm Global-Namespace und eine ausm std-Namespace mitm gleichen Namen usw. nicht ohne weiters unterscheiden kannst.</p>
<pre><code class="language-cpp">std::string
</code></pre>
<p>Naja ... <a href="http://www.cplusplus.com/reference/string/string/" rel="nofollow">http://www.cplusplus.com/reference/string/string/</a> ... ist halt sozusagen der C++ ersetz für einen char-array ... aber guck es dir mal genauer an ...</p>
<p><a href="http://www.highscore.de/" rel="nofollow">http://www.highscore.de/</a> hat nen ganz nettes Tutorial ... auch wenn die Codesnippets nicht immer 100% optimiert sind <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1340823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1340823</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 08 Aug 2007 13:35:37 GMT</pubDate></item></channel></rss>