<?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 mit fstream]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe 3 Funktionen geschrieben, mit der man Dateien &quot;komfortabler&quot; lesen bzw. ändern kann.<br />
Meine letzte Funktion, mit der man eine Zeile aus einer Datei löschen kann, funktioniert allerdings nicht richtig. Statt eine Zeile zu löschen, leert diese die ganze Datei.<br />
Meine Frage: Wo liegt der Fehler?</p>
<pre><code>void del_line (string file,long long int line) {
		/*	Löscht eine Zeile von einer externen Datei.
			Parameter [0] =&gt; Pfad zu der zu bearbeitenden Datei
			Parameter [1] =&gt; die Zeile, die gelöscht werden soll
			Rückgabewert: [void]
		*/
		string tbuffer_0;
		tbuffer_0 = &quot;&quot;;
		string tbuffer_1;
		tbuffer_1 = &quot;&quot;;
		long long int counter;
		counter = 0;
		bool fertig;
		fstream fs;
		fs.open (file.c_str (),fstream::in | fstream::out | fstream::trunc);
		fertig = fs.eof ();
		while ((fertig == false)) {
			getline (fs,tbuffer_0);
			if ((counter != line)) {
				tbuffer_1 += tbuffer_0 + &quot;\n&quot;;
			};
			counter += 1;
			fertig = fs.eof ();
		};
		fs &lt;&lt; tbuffer_1.c_str ();
	};
</code></pre>
<p>Danke im Voraus!</p>
<p>MfG</p>
<p>Seikuassi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/317678/problem-mit-fstream</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Jul 2026 14:21:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/317678.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Jun 2013 05:59:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 05:59:01 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe 3 Funktionen geschrieben, mit der man Dateien &quot;komfortabler&quot; lesen bzw. ändern kann.<br />
Meine letzte Funktion, mit der man eine Zeile aus einer Datei löschen kann, funktioniert allerdings nicht richtig. Statt eine Zeile zu löschen, leert diese die ganze Datei.<br />
Meine Frage: Wo liegt der Fehler?</p>
<pre><code>void del_line (string file,long long int line) {
		/*	Löscht eine Zeile von einer externen Datei.
			Parameter [0] =&gt; Pfad zu der zu bearbeitenden Datei
			Parameter [1] =&gt; die Zeile, die gelöscht werden soll
			Rückgabewert: [void]
		*/
		string tbuffer_0;
		tbuffer_0 = &quot;&quot;;
		string tbuffer_1;
		tbuffer_1 = &quot;&quot;;
		long long int counter;
		counter = 0;
		bool fertig;
		fstream fs;
		fs.open (file.c_str (),fstream::in | fstream::out | fstream::trunc);
		fertig = fs.eof ();
		while ((fertig == false)) {
			getline (fs,tbuffer_0);
			if ((counter != line)) {
				tbuffer_1 += tbuffer_0 + &quot;\n&quot;;
			};
			counter += 1;
			fertig = fs.eof ();
		};
		fs &lt;&lt; tbuffer_1.c_str ();
	};
</code></pre>
<p>Danke im Voraus!</p>
<p>MfG</p>
<p>Seikuassi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331668</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331668</guid><dc:creator><![CDATA[PHBU]]></dc:creator><pubDate>Mon, 17 Jun 2013 05:59:01 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 06:31:14 GMT]]></title><description><![CDATA[<p>Was macht fstream::trunc?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331677</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331677</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 17 Jun 2013 06:31:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Wed, 11 Jun 2014 23:22:56 GMT]]></title><description><![CDATA[<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331682</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331682</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Wed, 11 Jun 2014 23:22:56 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 22:00:38 GMT]]></title><description><![CDATA[<pre><code>// Includes
#include &lt;iostream&gt; // std::cout
#include &lt;string&gt;   // std::string, std::getline
#include &lt;fstream&gt;  // std::fstream

// Discard need of prepend std::
using namespace std;

// Delete one file line
bool del_line(string filename, long long int line){
    // Open file in read mode
    fstream file(filename, ios_base::in);
    if(!file)
        return false;
    // Text buffers
    string line_buffer, file_buffer;
    // Line counter
    long long int counter = 0;
    // Marker of line deletion
    bool line_deleted;
    // Read lines except not wished one
    while(getline(file, line_buffer))
        if(counter++ == line)
            line_deleted = true;
        else
            file_buffer += line_buffer + '\n';
    // Remove last newline
    if(file_buffer.size() &gt; 0)
        file_buffer.erase(file_buffer.size() - 1);
    // Deleted one line?
    if(!line_deleted)
        return false;
    // Reopen file in write mode without content
    file.close();
    file.open(filename, ios_base::out | ios_base::trunc);
    if(!file)
        return false;
    // Write new content to file
    file &lt;&lt; file_buffer;
    // Everything done!
    return true;
}

// Program entry
int main(){
    // Could delete first line of file &quot;input.txt&quot;?
    if(del_line(&quot;input.txt&quot;, 0))
        cout &lt;&lt; &quot;Success!&quot;;
    else
        cout &lt;&lt; &quot;Failed!&quot;;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331739</guid><dc:creator><![CDATA[Youka]]></dc:creator><pubDate>Mon, 17 Jun 2013 22:00:38 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 18:16:01 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<blockquote>
<p>Was macht fstream::trunc?</p>
</blockquote>
<p>&quot;trunc&quot; (aus dem Englischen truncate = kürzen) bedeutet, dass der Inhalt einer z.B. Datei ersetzt werden soll.<br />
&quot;app&quot; hingegen hängt dagegen den neuen Inhalt an die Datei an.</p>
<p>Hier nun meine aktuelle Version, die funktioniert:</p>
<pre><code>void del_line (string file,long long int line) {
		/*	Löscht eine Zeile von einer externen Datei.
			Parameter [0] =&gt; Pfad zu der Datei mit der zu löschenden Zile
			Parameter [1] =&gt; Zeile, die gelöscht werden soll
			Rückgabewert: [NONE]
		*/
		string tbuffer_0;
		tbuffer_0 = &quot;&quot;;
		string tbuffer_1;
		tbuffer_1 = &quot;&quot;;
		long long int counter;
		counter = 0;
		bool fertig;
		fstream fs;
		fs.open (file.c_str (),ios::in); // kein &quot;ios::trunc&quot; !!!
		fertig = fs.eof ();
		if ((line &gt; 0)) {
			while ((fertig == false)) {
				counter += 1;
				getline (fs,tbuffer_0);
				if ((counter != line)) {
					tbuffer_1 += tbuffer_0 + &quot;\n&quot;;
				};
				fertig = fs.eof ();
			};
			fs.close ();
			fs.open (file.c_str (),ios::in | ios::out | ios::trunc);
			fs &lt;&lt; tbuffer_1;
			fs.close ();
		};
	};
</code></pre>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/8250">@Swordfish</a>:<br />
Ihr habt zwar alle mehr Erfahrung als ich, aber in deiner Funktion öffnest du die Datei gleich mit &quot;ios:trunc&quot; auf. Hast du deine Funktion getestet?<br />
Meiner Meinung nach löscht du beim Aufrufen des Befehles die komplette Datei.<br />
Das war bei mir am Anfang auch mein Fehler. Ich habe mich gefragt, warum das am Anfang nicht ging. Bis ich dann eben bemerkt habe, dass ich die Datei zuerst nur mit &quot;ios::in&quot; aufrufen darf, um diese zu verarbeiten und dann erst die Datei überschreiben (mit eben &quot;ios::trunc&quot;) darf.</p>
<p>Danke nochmal für eure Ideen. Haben mir weitergeholfen.</p>
<p>MfG</p>
<p>Seikuassi</p>
<p>P.S.: Oh nein! Jetzt habe ich zu wenig Semikolone gsetzt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331913</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331913</guid><dc:creator><![CDATA[PHBU]]></dc:creator><pubDate>Mon, 17 Jun 2013 18:16:01 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 18:21:50 GMT]]></title><description><![CDATA[<p>Hallo nochmal!</p>
<p>Ich habe doch noch gerade ein Fehler entdeckt:<br />
Wenn ich eine Zeile lösche, dann wird das Escape-Zeichen &quot;newline&quot; (\n) nicht entfernt. Das heißt, dass die Zeile zwar keinen Inhalt mehr hat, aber diese trotzdem noch existiert.<br />
Wie kann ich nun das Escape-Zeichen mit entfernen?</p>
<p>Danke im Voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331914</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331914</guid><dc:creator><![CDATA[PHBU]]></dc:creator><pubDate>Mon, 17 Jun 2013 18:21:50 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 19:36:11 GMT]]></title><description><![CDATA[<p>Hallo nochmal nochmal <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>Ignoriert meinen gerade geschriebenen Beitrag. Ich bekomme immer nur am Ende der Datei eine leere Zeile.<br />
Wie kann ich aber diese leere Zeile löschen, also den letzten &quot;newline&quot; (\n) entfernen.</p>
<p>So ist es jetzt:</p>
<pre><code>[b]1:[/b] Hallo, das ist ein Test!
[b]2:[/b] Und noch ein Test!
[b]3:[/b] Und noch ein Test!
[b]4:[/b] Und noch ein Test!
[b]5:[/b]
</code></pre>
<p>Und ich möchte folgendes haben:</p>
<pre><code>[b]1:[/b] Hallo, das ist ein Test!
[b]2:[/b] Und noch ein Test!
[b]3:[/b] Und noch ein Test!
[b]4:[/b] Und noch ein Test!
</code></pre>
<p>Hier noch mal meine aktuelle Funktion:</p>
<pre><code>void del_line (string file,long long int line) {
		/*	Löscht eine Zeile von einer externen Datei.
			Parameter [0] =&gt; Pfad zu der Datei mit der zu löschenden Zeile
			Parameter [1] =&gt; Zeile, die gelöscht werden soll
			Rückgabewert: [void]
		*/
		string tbuffer_0;
		tbuffer_0 = &quot;&quot;;
		string tbuffer_1;
		tbuffer_1 = &quot;&quot;;
		long long int counter;
		counter = 0;
		bool fertig;
		fstream fs;
		fs.open (file.c_str (),ios::in);
		fertig = fs.eof ();
		if ((line &gt; 0)) {
			while ((fertig == false)) {
				counter += 1;
				getline (fs,tbuffer_0);
				if ((counter != line)) {
					tbuffer_1 += tbuffer_0 + &quot;\n&quot;; // das müsste den Fehler hervorgerufen haben durch \n am Ende
				};
				fertig = fs.eof ();
			};
			fs.close ();
			fs.open (file.c_str (),ios::in | ios::out | ios::trunc);
			fs &lt;&lt; tbuffer_1;
		};
		fs.close ();
	};
</code></pre>
<p>Danke im Voraus!</p>
<p>MfG</p>
<p>Seikuassi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331927</guid><dc:creator><![CDATA[PHBU]]></dc:creator><pubDate>Mon, 17 Jun 2013 19:36:11 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 20:45:10 GMT]]></title><description><![CDATA[<p>Murks.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331941</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Mon, 17 Jun 2013 20:45:10 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 22:01:47 GMT]]></title><description><![CDATA[<p>Lies doch die ganze Datei in einen string ein (insofern die Datei nicht übergroß ist).<br />
Dann suchst du nach den &quot;\n&quot;, bis du beim x. &quot;\n&quot; angekommen bist, wobei x die Zeilennummer, die zu löschen ist, ist.<br />
Ab da kannst du dann alles bis zum x-1. &quot;\n&quot; löschen (du kannst ja die einzelnen Chars vom string ansprechen).<br />
Dann kannst du dir auch sicher sein, dass das \n weg ist.<br />
Und zu guter Letzt schreibst du den abgeänderten string in ein leeres File.</p>
<p>Tadaa!</p>
<p>Ich hoffe, dass ich dir helfen konnte und nicht zu viel vorgesagt habe <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>ps.<br />
Deinen Code kannst um einiges kürzer und somit übersichtlicher machen:<br />
vorher:</p>
<pre><code>string tbuffer_0;
        tbuffer_0 = &quot;&quot;;
        string tbuffer_1;
        tbuffer_1 = &quot;&quot;;
        long long int counter;
        counter = 0;
        bool fertig;
        fstream fs;
        fs.open (file.c_str (),ios::in);
</code></pre>
<p>nachher:</p>
<pre><code>string tbuffer_0 = &quot;&quot;; oder string tbuffer_0(&quot;&quot;);
     string tbuffer_1 = &quot;&quot;;
     long long int counter = 0;
     bool fertig;
     fstream fs(file.c_str (),ios::in);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331981</guid><dc:creator><![CDATA[Schamote]]></dc:creator><pubDate>Mon, 17 Jun 2013 22:01:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Mon, 17 Jun 2013 22:09:36 GMT]]></title><description><![CDATA[<p>Ich hatte dir doch eine Lösung geschrieben, kommentiert und effektiv. Lies dir den 4ten Post nochmal durch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331983</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331983</guid><dc:creator><![CDATA[Youka]]></dc:creator><pubDate>Mon, 17 Jun 2013 22:09:36 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit fstream on Tue, 18 Jun 2013 12:44:03 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/30232">@Youka</a></p>
<p>Danke! Hab deinen Post erst später gelesen. Deine Funktion funktioniert prima!<br />
Danke für die Hilfe!</p>
<p>MfG</p>
<p>Seikuassi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2332142</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2332142</guid><dc:creator><![CDATA[PHBU]]></dc:creator><pubDate>Tue, 18 Jun 2013 12:44:03 GMT</pubDate></item></channel></rss>