From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/io%2Frename.html | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 devdocs/c/io%2Frename.html (limited to 'devdocs/c/io%2Frename.html') diff --git a/devdocs/c/io%2Frename.html b/devdocs/c/io%2Frename.html new file mode 100644 index 00000000..3997966b --- /dev/null +++ b/devdocs/c/io%2Frename.html @@ -0,0 +1,59 @@ +

rename

Defined in header <stdio.h>
int rename( const char* old_filename, const char* new_filename );
+

Changes the filename of a file. The file is identified by character string pointed to by old_filename. The new filename is identified by character string pointed to by new_filename.

+

If new_filename exists, the behavior is implementation-defined.

+

Parameters

+ + +
old_filename - pointer to a null-terminated string containing the path identifying the file to rename
new_filename - pointer to a null-terminated string containing the new path of the file

Return value

​0​ upon success or non-zero value on error.

+

Notes

POSIX specifies many additional details on the semantics of this function.

+

Example

#include <stdio.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    FILE* fp = fopen("from.txt", "w"); // create file "from.txt"
+    if (!fp)
+    {
+        perror("from.txt");
+        return EXIT_FAILURE;
+    }
+    fputc('a', fp); // write to "from.txt"
+    fclose(fp);
+ 
+    int rc = rename("from.txt", "to.txt");
+    if (rc)
+    {
+        perror("rename");
+        return EXIT_FAILURE;
+    }
+ 
+    fp = fopen("to.txt", "r");
+    if(!fp)
+    {
+        perror("to.txt");
+        return EXIT_FAILURE;
+    }
+    printf("%c\n", fgetc(fp)); // read from "to.txt"
+    fclose(fp);
+ 
+    return EXIT_SUCCESS;
+}

Possible output:

+
a

References

See also

+ +
erases a file
(function)
C++ documentation for rename
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/io/rename +

+
-- cgit v1.2.3