Rename LocalFileSystem flash memory files, thanks Doug Wendelboe for the fix

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mbed2f
Date:
Mon Apr 02 20:01:38 2012 +0000
Commit message:
LocalFileSystem with Rename

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
rename.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 07f9467c9d46 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Apr 02 20:01:38 2012 +0000
@@ -0,0 +1,14 @@
+
+ #include "mbed.h"
+ #include "rename.h"
+
+ LocalFileSystem local("local");             // Create the local filesystem under the name "local"
+
+ int main() {
+ FILE *fp = fopen("/local/Test.txt", "w");  // Open "out.txt" on the local file system for writing
+    fprintf(fp, "Hello World!");             
+    fclose(fp);                               
+     file_rename("/local/Test.txt", "/local/new_name.txt");
+    
+      
+    }
\ No newline at end of file
diff -r 000000000000 -r 07f9467c9d46 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Apr 02 20:01:38 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
diff -r 000000000000 -r 07f9467c9d46 rename.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rename.h	Mon Apr 02 20:01:38 2012 +0000
@@ -0,0 +1,67 @@
+  //***********************************************************
+// file_rename: 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 file_rename(const char *oldfname, const char *newfname) {
+    int retval = 0;
+    int ch;
+
+    FILE *fpold = fopen(oldfname, "r");   // src file
+    FILE *fpnew = fopen(newfname, "w");   // dest file
+    
+    while (1) {                   // Copy src to dest  
+        ch = fgetc(fpold);        // until src EOF read. 
+        if (ch == EOF) break;
+        fputc(ch, fpnew);  
+    }
+    
+    fclose(fpnew);
+    fclose(fpold);
+
+    fpnew = fopen(newfname, "r"); // Reopen dest to insure
+    if(fpnew == NULL) {           // that it was created.
+        retval = (-1);            // Return Error.
+    } 
+    else {
+        fclose(fpnew);  
+        remove(oldfname);         // Remove original file.
+        retval = (0);             // Return Success.
+    }
+    return (retval);
+}
+
+//***********************************************************
+// file_copy: Copies a file
+//            Checks to insure destination file was created.
+//            Returns -1 = error; 0 = success
+//***********************************************************
+int file_copy (const char *src, const char *dst) {
+    int retval = 0;
+    int ch;
+
+    FILE *fpsrc = fopen(src, "r");   // src file
+    FILE *fpdst = fopen(dst, "w");   // dest file
+    
+    while (1) {                  // Copy src to dest
+        ch = fgetc(fpsrc);       // until src EOF read.
+        if (ch == EOF) break;
+        fputc(ch, fpdst);  
+    }
+    fclose(fpsrc);  
+    fclose(fpdst);
+  
+    fpdst = fopen(dst, "r");     // Reopen dest to insure
+    if(fpdst == NULL) {          // that it was created.
+        retval = (-1);           // Return error.
+    } 
+    else {
+        fclose(fpdst); 
+        retval = (0);            // Return success.
+    }
+    return (retval);
+}
+
+    
+ 
\ No newline at end of file