Philips Philips
/
LocalFileSystem_with_Rename
Rename LocalFileSystem flash memory files, thanks Doug Wendelboe for the fix
rename.h@0:07f9467c9d46, 2012-04-02 (annotated)
- Committer:
- mbed2f
- Date:
- Mon Apr 02 20:01:38 2012 +0000
- Revision:
- 0:07f9467c9d46
LocalFileSystem with Rename
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed2f | 0:07f9467c9d46 | 1 | //*********************************************************** |
mbed2f | 0:07f9467c9d46 | 2 | // file_rename: renames a file (via copy & delete). |
mbed2f | 0:07f9467c9d46 | 3 | // Moves data instead of adjusting the file name in the |
mbed2f | 0:07f9467c9d46 | 4 | // file directory. Checks to insure the file was renamed. |
mbed2f | 0:07f9467c9d46 | 5 | // Returns -1 = error; 0 = success |
mbed2f | 0:07f9467c9d46 | 6 | //*********************************************************** |
mbed2f | 0:07f9467c9d46 | 7 | int file_rename(const char *oldfname, const char *newfname) { |
mbed2f | 0:07f9467c9d46 | 8 | int retval = 0; |
mbed2f | 0:07f9467c9d46 | 9 | int ch; |
mbed2f | 0:07f9467c9d46 | 10 | |
mbed2f | 0:07f9467c9d46 | 11 | FILE *fpold = fopen(oldfname, "r"); // src file |
mbed2f | 0:07f9467c9d46 | 12 | FILE *fpnew = fopen(newfname, "w"); // dest file |
mbed2f | 0:07f9467c9d46 | 13 | |
mbed2f | 0:07f9467c9d46 | 14 | while (1) { // Copy src to dest |
mbed2f | 0:07f9467c9d46 | 15 | ch = fgetc(fpold); // until src EOF read. |
mbed2f | 0:07f9467c9d46 | 16 | if (ch == EOF) break; |
mbed2f | 0:07f9467c9d46 | 17 | fputc(ch, fpnew); |
mbed2f | 0:07f9467c9d46 | 18 | } |
mbed2f | 0:07f9467c9d46 | 19 | |
mbed2f | 0:07f9467c9d46 | 20 | fclose(fpnew); |
mbed2f | 0:07f9467c9d46 | 21 | fclose(fpold); |
mbed2f | 0:07f9467c9d46 | 22 | |
mbed2f | 0:07f9467c9d46 | 23 | fpnew = fopen(newfname, "r"); // Reopen dest to insure |
mbed2f | 0:07f9467c9d46 | 24 | if(fpnew == NULL) { // that it was created. |
mbed2f | 0:07f9467c9d46 | 25 | retval = (-1); // Return Error. |
mbed2f | 0:07f9467c9d46 | 26 | } |
mbed2f | 0:07f9467c9d46 | 27 | else { |
mbed2f | 0:07f9467c9d46 | 28 | fclose(fpnew); |
mbed2f | 0:07f9467c9d46 | 29 | remove(oldfname); // Remove original file. |
mbed2f | 0:07f9467c9d46 | 30 | retval = (0); // Return Success. |
mbed2f | 0:07f9467c9d46 | 31 | } |
mbed2f | 0:07f9467c9d46 | 32 | return (retval); |
mbed2f | 0:07f9467c9d46 | 33 | } |
mbed2f | 0:07f9467c9d46 | 34 | |
mbed2f | 0:07f9467c9d46 | 35 | //*********************************************************** |
mbed2f | 0:07f9467c9d46 | 36 | // file_copy: Copies a file |
mbed2f | 0:07f9467c9d46 | 37 | // Checks to insure destination file was created. |
mbed2f | 0:07f9467c9d46 | 38 | // Returns -1 = error; 0 = success |
mbed2f | 0:07f9467c9d46 | 39 | //*********************************************************** |
mbed2f | 0:07f9467c9d46 | 40 | int file_copy (const char *src, const char *dst) { |
mbed2f | 0:07f9467c9d46 | 41 | int retval = 0; |
mbed2f | 0:07f9467c9d46 | 42 | int ch; |
mbed2f | 0:07f9467c9d46 | 43 | |
mbed2f | 0:07f9467c9d46 | 44 | FILE *fpsrc = fopen(src, "r"); // src file |
mbed2f | 0:07f9467c9d46 | 45 | FILE *fpdst = fopen(dst, "w"); // dest file |
mbed2f | 0:07f9467c9d46 | 46 | |
mbed2f | 0:07f9467c9d46 | 47 | while (1) { // Copy src to dest |
mbed2f | 0:07f9467c9d46 | 48 | ch = fgetc(fpsrc); // until src EOF read. |
mbed2f | 0:07f9467c9d46 | 49 | if (ch == EOF) break; |
mbed2f | 0:07f9467c9d46 | 50 | fputc(ch, fpdst); |
mbed2f | 0:07f9467c9d46 | 51 | } |
mbed2f | 0:07f9467c9d46 | 52 | fclose(fpsrc); |
mbed2f | 0:07f9467c9d46 | 53 | fclose(fpdst); |
mbed2f | 0:07f9467c9d46 | 54 | |
mbed2f | 0:07f9467c9d46 | 55 | fpdst = fopen(dst, "r"); // Reopen dest to insure |
mbed2f | 0:07f9467c9d46 | 56 | if(fpdst == NULL) { // that it was created. |
mbed2f | 0:07f9467c9d46 | 57 | retval = (-1); // Return error. |
mbed2f | 0:07f9467c9d46 | 58 | } |
mbed2f | 0:07f9467c9d46 | 59 | else { |
mbed2f | 0:07f9467c9d46 | 60 | fclose(fpdst); |
mbed2f | 0:07f9467c9d46 | 61 | retval = (0); // Return success. |
mbed2f | 0:07f9467c9d46 | 62 | } |
mbed2f | 0:07f9467c9d46 | 63 | return (retval); |
mbed2f | 0:07f9467c9d46 | 64 | } |
mbed2f | 0:07f9467c9d46 | 65 | |
mbed2f | 0:07f9467c9d46 | 66 | |
mbed2f | 0:07f9467c9d46 | 67 |