<?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 rename()]]></title><description><![CDATA[<p>Hallo, ich habe ein kleines Problem, das ich partout nicht lösen kann.<br />
Es geht darum, eine vorhandene Datei umzubenennen und in einen anderen Ordner zu verschieben. Der Dateiname soll dabei mit dem Datum versehen werden.<br />
Das passt eigentlich soweit auch ganz gut, nur klappt das eigentliche Umbenennen bei der Ausführung nicht. Ich bekomme immer den Fehler &quot;Invalid Argument&quot;. Vielleicht könnt Ihr mir da weiterhelfen. Vielen Dank!</p>
<pre><code class="language-cpp">#include &lt;time.h&gt;
#include &lt;string&gt;
#include &lt;stdio.h&gt;

using namespace std;

void main(){
	time_t rawtime;
	string filename;

	/*reads the systemtime and use the systemtime
	  to name the logfile with an unique name*/
	time (&amp;rawtime);
	string time = ctime(&amp;rawtime);

	/*delete weekday,blanks,seconds and year*/
	time.erase(0,4);
	for (int i=0;i&lt;5;i++)
		time.erase(3+(2*i),1);
	time.erase(9,6);

	/*build the new filename*/
	filename =&quot;C:/TOOLS/dat/log_pvr/fail&quot;;
	filename += time;
	filename +=&quot;.txt&quot;;

	const char* pNewFile= filename.c_str();

	int result= rename(&quot;C:/TOOLS/dat/fail.txt&quot;, pNewFile);
	if (result!=0)
		perror(&quot;Fehler:&quot;);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/173154/problem-mit-rename</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 04:29:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/173154.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Feb 2007 14:10:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 14:10:20 GMT]]></title><description><![CDATA[<p>Hallo, ich habe ein kleines Problem, das ich partout nicht lösen kann.<br />
Es geht darum, eine vorhandene Datei umzubenennen und in einen anderen Ordner zu verschieben. Der Dateiname soll dabei mit dem Datum versehen werden.<br />
Das passt eigentlich soweit auch ganz gut, nur klappt das eigentliche Umbenennen bei der Ausführung nicht. Ich bekomme immer den Fehler &quot;Invalid Argument&quot;. Vielleicht könnt Ihr mir da weiterhelfen. Vielen Dank!</p>
<pre><code class="language-cpp">#include &lt;time.h&gt;
#include &lt;string&gt;
#include &lt;stdio.h&gt;

using namespace std;

void main(){
	time_t rawtime;
	string filename;

	/*reads the systemtime and use the systemtime
	  to name the logfile with an unique name*/
	time (&amp;rawtime);
	string time = ctime(&amp;rawtime);

	/*delete weekday,blanks,seconds and year*/
	time.erase(0,4);
	for (int i=0;i&lt;5;i++)
		time.erase(3+(2*i),1);
	time.erase(9,6);

	/*build the new filename*/
	filename =&quot;C:/TOOLS/dat/log_pvr/fail&quot;;
	filename += time;
	filename +=&quot;.txt&quot;;

	const char* pNewFile= filename.c_str();

	int result= rename(&quot;C:/TOOLS/dat/fail.txt&quot;, pNewFile);
	if (result!=0)
		perror(&quot;Fehler:&quot;);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1227417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227417</guid><dc:creator><![CDATA[andrewz]]></dc:creator><pubDate>Mon, 12 Feb 2007 14:10:20 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 14:23:40 GMT]]></title><description><![CDATA[<p>andrewz schrieb:</p>
<blockquote>
<p>...klappt das eigentliche Umbenennen bei der Ausführung nicht. Ich bekomme immer den Fehler &quot;Invalid Argument&quot;....</p>
</blockquote>
<p>Hi,</p>
<p>&quot;Bei der Ausführung klappt nicht&quot; ? Was bedeutet das ?<br />
Wer liefert Dir den Fehler &quot;invalid Argument&quot; ? Das OS ?<br />
Mich überraschen die &quot;slashes&quot; (/) ein wenig in Kombination mit der Windowsnotation (C: ) ....</p>
<p>Außerdem finde ich das &quot;Mischen&quot; von C und C++ nicht so günstig (wenn ich auch verstehe, dass man auf das Ansi-C-rename() nicht ersatzlos verzichten möchte <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>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227425</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227425</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 12 Feb 2007 14:23:40 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 14:39:32 GMT]]></title><description><![CDATA[<p>Danke für die schnelle Antwort.<br />
Der Code wird problemlos kompiliert. Sobald ich jedoch über das Menü von Visual C++ das Programm ausführen möchte, bekomme ich im Konsolenfenster die Fehlermeldung.Diese lasse ich mir mit Hilfe von perror anzeigen, weil mir die Funktion rename immer nur -1 zurücklieferte. Das Programm wird eigentlich von einem TCL/TK Skript aufgerufen und wurde deshalb als Konsolenapplikation angelegt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227436</guid><dc:creator><![CDATA[andrewz]]></dc:creator><pubDate>Mon, 12 Feb 2007 14:39:32 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 14:50:14 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>hast Du schonmal versucht, die &quot;Windowskonvention für Pfadnamen&quot; zu verwenden ?<br />
Also statt slash ('/') backslash ('\') ?</p>
<p>Wichtig hierbei: Weil ein einfacher backslash als &quot;ecape-Character&quot; interpretiert wird, braucht's ihn doppelt:</p>
<pre><code class="language-cpp">filename =&quot;C:\\TOOLS\\dat\\log_pvr\\fail&quot;;
</code></pre>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227443</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 12 Feb 2007 14:50:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 15:07:05 GMT]]></title><description><![CDATA[<p>Hallo, leider ändert sich auch bei der neuen Notation das Ergebnis nicht.<br />
Ich habe aus &quot;Spaß&quot; mal die Pfadangabe direkt in den rename-Aufruf eingetragen:</p>
<pre><code class="language-cpp">int result = rename(&quot;C:\\Tools\\dat\\fail.txt&quot;,&quot;C:\\Tools\\dat\\log_pvr\\fail2.txt&quot;);
</code></pre>
<p>So funktioniert es. Aber das ist leider nicht das, was ich benötige.Ich brauch eine Möglichkeit, um die Datei zu verschieben und mit einem eindeutigen Namen zu versehen.<br />
Vielleicht sollte ich die Datei einfach mit mit Hilfe eines Streams einlesen und &quot;an der richtigen Stelle&quot; wieder ausgeben.</p>
<p>Gruß André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227465</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227465</guid><dc:creator><![CDATA[andrewz]]></dc:creator><pubDate>Mon, 12 Feb 2007 15:07:05 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 15:19:47 GMT]]></title><description><![CDATA[<p>andrewz schrieb:</p>
<blockquote>
<p>Hallo, leider ändert sich auch bei der neuen Notation das Ergebnis nicht.<br />
Ich habe aus &quot;Spaß&quot; mal die Pfadangabe direkt in den rename-Aufruf eingetragen:</p>
<pre><code class="language-cpp">int result = rename(&quot;C:\\Tools\\dat\\fail.txt&quot;,&quot;C:\\Tools\\dat\\log_pvr\\fail2.txt&quot;);
</code></pre>
<p>So funktioniert es. ...</p>
</blockquote>
<p>Dann liegt es mit ziemlicher Sicherheit an dem &quot;falschen slash&quot; + Du machst noch einen anderen Fehler.</p>
<p>Hast Du mal Das Ergebnis von</p>
<pre><code class="language-cpp">filename =&quot;C:\\TOOLS\\dat\\log_pvr\\fail&quot;;
    filename += time;
    filename +=&quot;.txt&quot;;
</code></pre>
<p>ausgegeben ?</p>
<p>Ach ja:</p>
<pre><code class="language-cpp">const char* pNewFile= filename.c_str();
</code></pre>
<p>Den braucht's nicht.<br />
Du kannst direkt schreiben:</p>
<pre><code class="language-cpp">int result= rename(&quot;C:\\TOOLS\\dat\\fail.txt&quot;, filename.c_str());
</code></pre>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227476</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 12 Feb 2007 15:19:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 15:50:11 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a><br />
Windows akzeptiert auch die normalen Slashs (natürlich nicht doppelt) in Pfadangaben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227497</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 12 Feb 2007 15:50:11 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 16:17:07 GMT]]></title><description><![CDATA[<p>Ah ... stimmt !!</p>
<p>Kann es sein, dass ctime() am Ende einen Zeilenumbruch anhängt ?</p>
<p>Außerdem ist es ungünstig, eine Variable (&quot;time&quot;) genauso zu nennen wie ein Funktion...</p>
<p>mein Vorschlag:</p>
<pre><code class="language-cpp">int main() {
    time_t rawtime;
    string fromFilename;
    string toFilename;

    /*reads the systemtime and use the systemtime
      to name the logfile with an unique name*/
    time (&amp;rawtime);
    string timeStr = ctime(&amp;rawtime);

    /*delete weekday,blanks,seconds and year*/
    timeStr.erase(0,4);
    for (int i=0;i&lt;5;i++)
        timeStr.erase(3+(2*i),1);
    timeStr.erase(9); // Alles nach Pos 9 löschen

    fromFilename = &quot;C:/tools/dat/fail.txt&quot;;
    toFilename =&quot;C:/tools/dat/log_pvr/fail&quot; + timeStr + &quot;.txt&quot;;

    int result= rename(fromFilename.c_str(), toFilename.c_str());
    if (result!=0)
        perror(&quot;Fehler:&quot;);
   return 0;
</code></pre>
<p>funktioniert jedenfalls bei mir...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227507</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227507</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 12 Feb 2007 16:17:07 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 16:11:22 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Ah ... kann es sein, dass ctime() am Ende einen Zeilenumbruch anhängt ?</p>
</blockquote>
<p>So ist es:</p>
<p>C++ Reference (<a href="http://cplusplus.com" rel="nofollow">cplusplus.com</a>) schrieb:</p>
<blockquote>
<p>The string is followed by a new-line character ('\n') and the terminating null-character.</p>
</blockquote>
<p>Greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227519</guid><dc:creator><![CDATA[Vellas]]></dc:creator><pubDate>Mon, 12 Feb 2007 16:11:22 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 16:51:29 GMT]]></title><description><![CDATA[<p>Braunstein schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a><br />
Windows akzeptiert auch die normalen Slashs (natürlich nicht doppelt) in Pfadangaben.</p>
</blockquote>
<p>Hi,</p>
<p>ist das eigentlich eine Eigenschaft von Windows ? Oder von einer bestimmten StdLib ? Oder ... ?</p>
<p>Im Standard habe ich jedenfalls keine Aussage dazu gefunden, wie systemspezifische Pfad-/Filenamen zu handhaben seien.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227568</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227568</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 12 Feb 2007 16:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 17:12:04 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a><br />
Herzlichen Dank für die Hilfe! Mit Deinem Vorschlag funktioniert es jetzt.</p>
<p>Gruß André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227598</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227598</guid><dc:creator><![CDATA[andrewz]]></dc:creator><pubDate>Mon, 12 Feb 2007 17:12:04 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Mon, 12 Feb 2007 19:20:16 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Braunstein schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13624">@Simon2</a><br />
Windows akzeptiert auch die normalen Slashs (natürlich nicht doppelt) in Pfadangaben.</p>
</blockquote>
<p>Hi,</p>
<p>ist das eigentlich eine Eigenschaft von Windows ? Oder von einer bestimmten StdLib ? Oder ... ?</p>
<p>Im Standard habe ich jedenfalls keine Aussage dazu gefunden, wie systemspezifische Pfad-/Filenamen zu handhaben seien.</p>
</blockquote>
<p>Das funktioniert ganz allgemein in Windows. Im Explorer beispielsweise kannst du auch Pfade mit einem Slash ansteuern, du kannst auch Slash mit Backslashes mischen. Das ist ganz gut bei Headern, da kann man dann auch '/' statt '\' benutzen (oder ging das da schon immer?). Auf jedenfall hab ich nirgends Probleme mit der Verwendung von Slashes. Ich glaub das geht sogar in der cmd.exe (zumindest bei Vista) und in der PowerShell ja sowieso. Ich persönlich finds gut das es geht.</p>
<p>Greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227695</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227695</guid><dc:creator><![CDATA[Vellas]]></dc:creator><pubDate>Mon, 12 Feb 2007 19:20:16 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Tue, 13 Feb 2007 08:20:48 GMT]]></title><description><![CDATA[<p>Vellas schrieb:</p>
<blockquote>
<p>...<br />
Das funktioniert ganz allgemein in Windows. ...</p>
</blockquote>
<p>Aha !!!</p>
<p>Vielen Dank. Eigentlich finde ich das auch gut (löst ein Plattformspezifikum teilweise) ... ein wenig skeptisch bin ich bei einem Randthema (das sich bei Std-C++ zum Glück nicht stellt): Wenn man die Möglichkeit hat/hätte, den Namen eines Files herauszubekommen, könnte das zu unerwartetem Verhalten kommen:</p>
<pre><code class="language-cpp">string fileName(&quot;MyPath/MyFile.txt&quot;);
ifstream in(fileName.c_str());

if(myGetFileName(in) != fileName) {
    cout &lt;&lt; &quot;Huuups, wie kann das sein ?\n&quot;;
}
</code></pre>
<p>myGetFileName() kann es eigentlich nur falsch machen: Entweder gibt es den Namen windowspezifisch ('\') raus, dann knallt's bei obigem Code ... oder er gibt es Unix-spezifisch raus, dann kracht's bei &quot;echten Windowsfilenamen&quot;...<br />
Einzige Alternative: myGetFileName() gibt den Namen raus, mit dem das File geöffnet wurde ... aber da wird man wohl einiges an Fingerbrechen für veranstalten müssen (und da konsequent auch so Sachen wie rename() etc. unterzubringen, halte ich auch für sehr schwierig).</p>
<p>Danke für die Info,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227904</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Tue, 13 Feb 2007 08:20:48 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Tue, 13 Feb 2007 08:45:30 GMT]]></title><description><![CDATA[<p>Stimmt genau. Diese doppeldeutigkeit führt eigentlich mehr zu Problemen.<br />
Egal wie man es bevorzugt, als Programmierer in Windows muss man auf diese Gegebenheit halt irgendwie reagieren. Sei es nun den Pfad zu splitten und dann zu vergleichen oder im Programm alle Pfade in eine der beiden Möglichkeiten umwandeln.</p>
<p>Mit boost::path kann man sich da aber schon etwas an Arbeit ersparen. Da kann man auch beides nutzen (wenn man native verwendet).</p>
<p>Greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227912</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227912</guid><dc:creator><![CDATA[Vellas]]></dc:creator><pubDate>Tue, 13 Feb 2007 08:45:30 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Tue, 13 Feb 2007 09:58:34 GMT]]></title><description><![CDATA[<p>Genau deswegen nutze ich nur noch boost::filesystem::path und Konsorten. Ich mußte mal ein Programm auf einem japanischen Rechner zum laufen bringen. Dort nehmen sie das Yen-Zeichen als Slash. <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/1227947</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227947</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Tue, 13 Feb 2007 09:58:34 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit rename() on Tue, 13 Feb 2007 10:27:36 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>da ich gerade mit einem Arbeitskollegen drüber diskutierte: Wie macht Jxxx (um mal das &quot;böse Wort&quot; zu vermeiden <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="😉"
    /> ) das eigentlich ?<br />
Seine Aussage war:<br />
- Da kann man immer auch mit '/' arbeiten und<br />
- man kann bei einem &quot;File&quot; auch dessen (Pfad)Namen abfragen.<br />
Allerdings wusste er auch nicht, ob man dann die '/'- oder die '\'-Variante oder etwas ganz Anderes bekommt.<br />
Weiß das hier zufällig jemand ?<br />
Bitte bitte bitte hier keinen Flame ... ich will nur wissen, wie die das machen.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1227970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1227970</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Tue, 13 Feb 2007 10:27:36 GMT</pubDate></item></channel></rss>