<?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[fstream und Dateipfade]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe das Problem, dass wenn ich mit fstream Arbeite zwar Dateinamen problemlos eingeben kann, aber bei Dateipfaden Probleme bekomme.</p>
<p>Vorerst das System:<br />
Betriebssystem: Linux<br />
Compiler: g++</p>
<p>Also ich habe eine Klasse ExampleDocument</p>
<pre><code>class ExampleDocument {
	private:
		char* path;		
		string inputfile;
		string outputfile;

	public:		
		ExampleDocument();
		ExampleDocument(bool newdocument, bool typeInput);
		ExampleDocument(string path, bool newdocument, bool typeInput);
		virtual ~ExampleDocument();

		int WriteDocument(string textvalue);
};
</code></pre>
<p>Die Methode &quot;WriteDocument&quot; funktioniert im ersten Durchlauf, im zweiten und in weiteren Durchläufen wird die Variable &quot;path&quot; verändert.</p>
<p>Die Methode WriteDocument:</p>
<pre><code>int ExampleDocument::WriteDocument(string textvalue){
	fstream f;

	cout &lt;&lt; &quot;Pfad1: &quot; &lt;&lt; this-&gt;path &lt;&lt; endl;	

	f.open(this-&gt;path, ios::app|ios::out);
	f &lt;&lt; textvalue &lt;&lt; endl;
	f.close();

	cout &lt;&lt; &quot;Pfad2: &quot; &lt;&lt; this-&gt;path &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Der Verwendete Konstruktor:</p>
<pre><code>ExampleDocument::ExampleDocument(string tmp_path, bool newdocument, bool typeInput) {
	this-&gt;inputfile=&quot;inputFile.dat&quot;;
	this-&gt;outputfile=&quot;outputFile.dat&quot;;
	string file;
	string tmp_string;

	if(typeInput){
		file = this-&gt;inputfile;
	}else{
		file = this-&gt;outputfile;	
	}

	tmp_string = tmp_path+file;

	this-&gt;path = const_cast&lt;char*&gt;(tmp_string.c_str());

	if(newdocument){
		fstream fs(this-&gt;path, ios::out);
		fs.close();	
	}
}
</code></pre>
<p>Die &quot;path&quot; Variable wird direkt nach dem Schreiben in die Datei verändert.<br />
Die Ausgabe lautet:<br />
Pfad1: /home/einVerzeichnis/eineDatei.dat (korrekt)<br />
Pfad2: /home/einVerzeichnis/eineDatei.dat0 (falsch)</p>
<p>Vielen Dank!</p>
<p>Gruß</p>
<p>mustino</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/193502/fstream-und-dateipfade</link><generator>RSS for Node</generator><lastBuildDate>Mon, 28 Sep 2026 02:18:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/193502.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Sep 2007 16:06:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:06:45 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe das Problem, dass wenn ich mit fstream Arbeite zwar Dateinamen problemlos eingeben kann, aber bei Dateipfaden Probleme bekomme.</p>
<p>Vorerst das System:<br />
Betriebssystem: Linux<br />
Compiler: g++</p>
<p>Also ich habe eine Klasse ExampleDocument</p>
<pre><code>class ExampleDocument {
	private:
		char* path;		
		string inputfile;
		string outputfile;

	public:		
		ExampleDocument();
		ExampleDocument(bool newdocument, bool typeInput);
		ExampleDocument(string path, bool newdocument, bool typeInput);
		virtual ~ExampleDocument();

		int WriteDocument(string textvalue);
};
</code></pre>
<p>Die Methode &quot;WriteDocument&quot; funktioniert im ersten Durchlauf, im zweiten und in weiteren Durchläufen wird die Variable &quot;path&quot; verändert.</p>
<p>Die Methode WriteDocument:</p>
<pre><code>int ExampleDocument::WriteDocument(string textvalue){
	fstream f;

	cout &lt;&lt; &quot;Pfad1: &quot; &lt;&lt; this-&gt;path &lt;&lt; endl;	

	f.open(this-&gt;path, ios::app|ios::out);
	f &lt;&lt; textvalue &lt;&lt; endl;
	f.close();

	cout &lt;&lt; &quot;Pfad2: &quot; &lt;&lt; this-&gt;path &lt;&lt; endl;

	return 0;
}
</code></pre>
<p>Der Verwendete Konstruktor:</p>
<pre><code>ExampleDocument::ExampleDocument(string tmp_path, bool newdocument, bool typeInput) {
	this-&gt;inputfile=&quot;inputFile.dat&quot;;
	this-&gt;outputfile=&quot;outputFile.dat&quot;;
	string file;
	string tmp_string;

	if(typeInput){
		file = this-&gt;inputfile;
	}else{
		file = this-&gt;outputfile;	
	}

	tmp_string = tmp_path+file;

	this-&gt;path = const_cast&lt;char*&gt;(tmp_string.c_str());

	if(newdocument){
		fstream fs(this-&gt;path, ios::out);
		fs.close();	
	}
}
</code></pre>
<p>Die &quot;path&quot; Variable wird direkt nach dem Schreiben in die Datei verändert.<br />
Die Ausgabe lautet:<br />
Pfad1: /home/einVerzeichnis/eineDatei.dat (korrekt)<br />
Pfad2: /home/einVerzeichnis/eineDatei.dat0 (falsch)</p>
<p>Vielen Dank!</p>
<p>Gruß</p>
<p>mustino</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373238</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373238</guid><dc:creator><![CDATA[mustino]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:06:45 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:12:10 GMT]]></title><description><![CDATA[<p>mustino schrieb:</p>
<blockquote>
<p>...</p>
<pre><code>class ExampleDocument {
	private:
		char* path;
</code></pre>
</blockquote>
<p>Warum nicht string ?</p>
<p>mustino schrieb:</p>
<blockquote>
<p>...</p>
<pre><code>f.open(this-&gt;path, ios::app|ios::out);
</code></pre>
</blockquote>
<p>Das würde ich nicht machen, weil man nunmal keine &quot;Pfade öffnen&quot; kann, sondern nur Files (heißt ja <strong>f</strong>stream und nicht <strong>p</strong>stream <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 />
Ob das die Ursache für das Problem ist, weiß ich nicht, weil ich nicht sehe, wie die Klasse benutzt wird. Aber es sieht mir &quot;gefährlich&quot; aus...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373246</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:12:10 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:18:15 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>danke vorerst fü die schnelle Antwort.</p>
<p>Gibt es denn eine andere Möglichkeit eine Datei mit Pfadangabe zu öffnen und zu beschreiben???</p>
<p>Gruß,</p>
<p>mustino</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373251</guid><dc:creator><![CDATA[mustino]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:18:15 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:19:54 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">ExampleDocument::ExampleDocument(std::string const&amp; path, bool newdoc, bool type) : inputfile(&quot;inputFile.dat&quot;), outputfile(&quot;outputFile.dat&quot;)
{
    std::string tmp(path);
    tmp += (type == true ? inputfile : outputfile);

    this-&gt;path = new char[tmp.length() + 1];
    std::strcpy(this-&gt;path, tmp.c_str());

    if (newdoc)
        std::ofstream file_stream(this-&gt;path);
}

ExampleDocument::~ExampleDocument()
{
    delete [] path;
}
</code></pre>
<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373255</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:19:54 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:22:42 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/734">@Simon</a>: Nee das ist nicht das Problem <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="😃"
    /> Er macht sehr viel Scheiße in 10 Zeilen-Code ... das ist das Problem <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="😃"
    /> Er legt ein temporäres Objekt vom Type std::string an. Holt sich dann einen const. char-Zeiger von diesem Objekt (und castet das verdammte const. auchnoch weg!!! ?! ). Dann verlässt er den Konstruktor. Das temporäre Objekt wird freigegeben. Der Zeiger zeigt sonnst wo hin <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="😉"
    /> Und damit auch sein this-&gt;path, denn er hat nicht den Inhalt des char-Arrays kopiert sondern nur den Zeiger umgelenkt <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/1373257</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373257</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:22:42 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Wed, 26 Sep 2007 16:47:58 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>danke für die Hilfe (die war lehrreich!). Das ich nur den Zeiger umgelenkt und nicht den Inhalt kopiert habe, darauf hätte ich kommen müssen. Aus Fehlern lernt man...</p>
<p>Danke.</p>
<p>mustino</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373283</guid><dc:creator><![CDATA[mustino]]></dc:creator><pubDate>Wed, 26 Sep 2007 16:47:58 GMT</pubDate></item><item><title><![CDATA[Reply to fstream und Dateipfade on Thu, 27 Sep 2007 08:48:11 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<p>...Er macht sehr viel Scheiße in 10 Zeilen-Code ...</p>
</blockquote>
<p>Japp - aber mit string wär das nicht passiert. <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>
<p>Deswegen wiederhole ich meine Frage:</p>
<p>Simon2 schrieb:</p>
<blockquote>
<p>mustino schrieb:</p>
<blockquote>
<p>...</p>
<pre><code>class ExampleDocument {
	private:
		char* path;
</code></pre>
</blockquote>
<p>Warum nicht string ?</p>
</blockquote>
<p>Das hätte aus</p>
<pre><code class="language-cpp">ExampleDocument::ExampleDocument(string tmp_path, bool newdocument, bool typeInput) {
    this-&gt;inputfile=&quot;inputFile.dat&quot;;
    this-&gt;outputfile=&quot;outputFile.dat&quot;;
    string file;
    string tmp_string;

    if(typeInput){
        file = this-&gt;inputfile;
    }else{
        file = this-&gt;outputfile;   
    }

    tmp_string = tmp_path+file;

    this-&gt;path = const_cast&lt;char*&gt;(tmp_string.c_str());

    if(newdocument){
        fstream fs(this-&gt;path, ios::out);
        fs.close();   
    }
}
</code></pre>
<p>=&gt;</p>
<pre><code class="language-cpp">ExampleDocument::ExampleDocument(string tmp_path, bool newdocument, bool typeInput) {
    inputfile=&quot;inputFile.dat&quot;;
    outputfile=&quot;outputFile.dat&quot;;

    string file = path;
    file += typeInput ? outputfile : inputfile; 
    if(newdocument){
        fstream fs(file.c_str(), ios::out);
        fs.close();   
    }
}
</code></pre>
<p>gemacht - was</p>
<p>Übrigens1: Im Originalcode wird die Membervariable path jedesmal um einen Dateinamen erweitert:<br />
1. Call -&gt; &quot;\meinDir\irgendwo\inputFile.dat&quot;<br />
2. Call -&gt; &quot;\meinDir\irgendwo\inputFile.datinputFile.dat&quot;<br />
3. Call -&gt; &quot;\meinDir\irgendwo\inputFile.datinputFile.datinputFile.dat&quot;<br />
...<br />
ich glaube nicht, dass das gewollt ist (habe das deswegen in meinem Code rausgemacht).</p>
<p>Übrigens2: Da sich Input- und Outputfile nicht nur im Namen unterscheiden, sondern auch im Zugriff(verschiedene &quot;openmodes&quot;, mal read mal write, ....), würde ich nicht versuchen, dieses Aufgaben &quot;zusammenzulegen&quot;.</p>
<p>Ansonsten gäbe es noch einiges zum Design zu sagen, aber das ist noch ein weites Feld, auf dem Du vermutlich erstmal selbst ein wenig Erfahrung sammeln solltest...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1373658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1373658</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 27 Sep 2007 08:48:11 GMT</pubDate></item></channel></rss>