Utility for copying and renaming files.

Utility for copying and renaming files.

Revision:
0:4be393eec2a2
Child:
1:1f1e0c92b3f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FileUtils.h	Mon Dec 30 20:36:41 2013 +0000
@@ -0,0 +1,41 @@
+
+/** fcopy: Copies a file
+ *            Checks to insure destination file was created.
+ *            Returns -1 = error; 0 = success
+ */
+int fcopy (const char *src, const char *dst) { 
+    FILE *fpsrc = fopen(src, "r");
+    FILE *fpdst = fopen(dst, "w");
+    
+    int ch = fgetc(fpsrc);
+    while (ch != EOF) {
+        fputc(ch, fpdst);  
+        ch = fgetc(fpsrc);   
+    }
+    
+    fclose(fpsrc);  
+    fclose(fpdst);
+    
+    int retval = 0;
+    fpdst = fopen(dst, "r");
+    if (fpdst == NULL) {
+        retval = -1;
+    } else {
+        fclose(fpdst); 
+        retval = 0;
+    }
+    return retval;
+}
+
+/** frename: renames a file (via copy & delete).
+ *    Moves data instead of adjusting the file name in the
+ *    file directory. Checks to insure the file was renamed.
+ *    Returns -1 = error; 0 = success
+ */
+int frename(const char *oldfname, const char *newfname) {
+    int retval = 0;
+    if (fcopy(oldfname, newfname)) {
+        remove(oldfname);    
+    }     
+    return retval;
+}
\ No newline at end of file