Example writing to SD card, sford (original) 21 June 2011: Enhanced by D. Wendelboe to test SD file and directories. Illustrates the following: * rename a file (via copy & delete old). * copy an existing file to a new file. * listing of SD directory names. TODO: Add method to list filenames in a directory. Add a method to return file length.
main.cpp@1:4a0985d3a848, 2011-06-22 (annotated)
- Committer:
- YouTahDoug
- Date:
- Wed Jun 22 16:52:20 2011 +0000
- Revision:
- 1:4a0985d3a848
- Parent:
- 0:d0eea54553ed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YouTahDoug | 1:4a0985d3a848 | 1 | //***************************************************************************** |
YouTahDoug | 1:4a0985d3a848 | 2 | // Example writing to SD card, sford |
YouTahDoug | 1:4a0985d3a848 | 3 | // |
YouTahDoug | 1:4a0985d3a848 | 4 | // 21 June 2011: Enhanced by D. Wendelboe to test SD file and directories. |
YouTahDoug | 1:4a0985d3a848 | 5 | // Illustrates the following: |
YouTahDoug | 1:4a0985d3a848 | 6 | // * rename a file (via copy & delete old). |
YouTahDoug | 1:4a0985d3a848 | 7 | // * copy an existing file to a new file. |
YouTahDoug | 1:4a0985d3a848 | 8 | // * listing of SD directory names. |
YouTahDoug | 1:4a0985d3a848 | 9 | // |
YouTahDoug | 1:4a0985d3a848 | 10 | // TODO: Add method to list filenames in a directory. |
YouTahDoug | 1:4a0985d3a848 | 11 | // Add a method to return file length. |
YouTahDoug | 1:4a0985d3a848 | 12 | //***************************************************************************** |
YouTahDoug | 1:4a0985d3a848 | 13 | |
YouTahDoug | 1:4a0985d3a848 | 14 | #include "mbed.h" |
YouTahDoug | 1:4a0985d3a848 | 15 | #include "SDFileSystem.h" |
YouTahDoug | 1:4a0985d3a848 | 16 | |
YouTahDoug | 1:4a0985d3a848 | 17 | #define PC_BAUD 38400 |
YouTahDoug | 1:4a0985d3a848 | 18 | |
YouTahDoug | 1:4a0985d3a848 | 19 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
YouTahDoug | 1:4a0985d3a848 | 20 | Serial pc(USBTX,USBRX); |
YouTahDoug | 1:4a0985d3a848 | 21 | |
YouTahDoug | 1:4a0985d3a848 | 22 | int file_rename(const char *oldfname, const char *newfname); |
YouTahDoug | 1:4a0985d3a848 | 23 | int file_copy(const char *src, const char *dst); |
YouTahDoug | 1:4a0985d3a848 | 24 | |
YouTahDoug | 1:4a0985d3a848 | 25 | int main() |
YouTahDoug | 1:4a0985d3a848 | 26 | { |
YouTahDoug | 1:4a0985d3a848 | 27 | int i, status; |
YouTahDoug | 1:4a0985d3a848 | 28 | pc.baud(PC_BAUD); |
YouTahDoug | 1:4a0985d3a848 | 29 | |
YouTahDoug | 1:4a0985d3a848 | 30 | printf("\nExercise SD Card functions\n"); |
YouTahDoug | 1:4a0985d3a848 | 31 | |
YouTahDoug | 1:4a0985d3a848 | 32 | printf("Create 3 directories on SD Card.\n"); |
YouTahDoug | 1:4a0985d3a848 | 33 | mkdir("/sd/mydir1", 0777); |
YouTahDoug | 1:4a0985d3a848 | 34 | mkdir("/sd/mydir2", 0777); |
YouTahDoug | 1:4a0985d3a848 | 35 | mkdir("/sd/mydir3", 0777); |
YouTahDoug | 1:4a0985d3a848 | 36 | |
YouTahDoug | 1:4a0985d3a848 | 37 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 38 | printf("Create \"/sd/mydir1/sdtest.txt\" and write some data in it\n"); |
YouTahDoug | 1:4a0985d3a848 | 39 | |
YouTahDoug | 1:4a0985d3a848 | 40 | FILE *fp = fopen("/sd/mydir1/sdtest.txt", "w"); |
YouTahDoug | 1:4a0985d3a848 | 41 | if(fp == NULL) { |
YouTahDoug | 1:4a0985d3a848 | 42 | error("Could not open file for write\n"); |
YouTahDoug | 1:4a0985d3a848 | 43 | } |
YouTahDoug | 1:4a0985d3a848 | 44 | fprintf(fp, "Hello SD Card World!\n"); |
YouTahDoug | 1:4a0985d3a848 | 45 | for (i=0; i<10; i++) { |
YouTahDoug | 1:4a0985d3a848 | 46 | fprintf(fp, "Line #%i\n", i); |
YouTahDoug | 1:4a0985d3a848 | 47 | } |
YouTahDoug | 1:4a0985d3a848 | 48 | fclose(fp); |
YouTahDoug | 1:4a0985d3a848 | 49 | |
YouTahDoug | 1:4a0985d3a848 | 50 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 51 | printf("Open directory on /sd\n"); |
YouTahDoug | 1:4a0985d3a848 | 52 | DIR *d = opendir("/sd"); |
YouTahDoug | 1:4a0985d3a848 | 53 | struct dirent *p; |
YouTahDoug | 1:4a0985d3a848 | 54 | while((p = readdir(d)) != NULL) { |
YouTahDoug | 1:4a0985d3a848 | 55 | printf("%s\n", p->d_name); |
YouTahDoug | 1:4a0985d3a848 | 56 | } |
YouTahDoug | 1:4a0985d3a848 | 57 | closedir(d); |
YouTahDoug | 1:4a0985d3a848 | 58 | printf("Directory closed\n"); |
YouTahDoug | 1:4a0985d3a848 | 59 | |
YouTahDoug | 1:4a0985d3a848 | 60 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 61 | printf("Remove /sd/mydir3\n"); |
YouTahDoug | 1:4a0985d3a848 | 62 | remove("/sd/mydir3"); |
YouTahDoug | 1:4a0985d3a848 | 63 | |
YouTahDoug | 1:4a0985d3a848 | 64 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 65 | printf("List directory again. mydir3 should be gone.\n"); |
YouTahDoug | 1:4a0985d3a848 | 66 | d = opendir("/sd"); |
YouTahDoug | 1:4a0985d3a848 | 67 | while((p = readdir(d)) != NULL) { |
YouTahDoug | 1:4a0985d3a848 | 68 | printf("%s\n", p->d_name); |
YouTahDoug | 1:4a0985d3a848 | 69 | } |
YouTahDoug | 1:4a0985d3a848 | 70 | closedir(d); |
YouTahDoug | 1:4a0985d3a848 | 71 | |
YouTahDoug | 1:4a0985d3a848 | 72 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 73 | printf("Rename \"/sd/mydir1/sdtest.txt\" to \"/sd/mydir1/new_name.txt\"\n"); |
YouTahDoug | 1:4a0985d3a848 | 74 | status = file_rename("/sd/mydir1/sdtest.txt", "/sd/mydir1/new_name.txt"); |
YouTahDoug | 1:4a0985d3a848 | 75 | |
YouTahDoug | 1:4a0985d3a848 | 76 | if (status == -1) printf("Error: file not renamed. (status=%d)\n", status); |
YouTahDoug | 1:4a0985d3a848 | 77 | else printf("Success: file renamed OK (status=%d)\n", status); |
YouTahDoug | 1:4a0985d3a848 | 78 | |
YouTahDoug | 1:4a0985d3a848 | 79 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 80 | printf("Copy \"/sd/mydir1/doug.txt\" to \"/sd/mydir1/new_copy.txt\"\n"); |
YouTahDoug | 1:4a0985d3a848 | 81 | status = file_copy("/sd/mydir1/new_name.txt", "/sd/mydir1/new_copy.txt"); |
YouTahDoug | 1:4a0985d3a848 | 82 | |
YouTahDoug | 1:4a0985d3a848 | 83 | if (status == -1) printf("Error: file not copied. (status=%d)\n", status); |
YouTahDoug | 1:4a0985d3a848 | 84 | else printf("Success: file copied OK (status=%d)\n", status); |
YouTahDoug | 1:4a0985d3a848 | 85 | |
YouTahDoug | 1:4a0985d3a848 | 86 | //-------------------------------------------------------------- |
YouTahDoug | 1:4a0985d3a848 | 87 | printf("Done.\n"); |
YouTahDoug | 1:4a0985d3a848 | 88 | } |
YouTahDoug | 1:4a0985d3a848 | 89 | |
YouTahDoug | 1:4a0985d3a848 | 90 | //*********************************************************** |
YouTahDoug | 1:4a0985d3a848 | 91 | // file_rename: renames a file (via copy & delete). |
YouTahDoug | 1:4a0985d3a848 | 92 | // Moves data instead of adjusting the file name in the |
YouTahDoug | 1:4a0985d3a848 | 93 | // file directory. Checks to insure the file was renamed. |
YouTahDoug | 1:4a0985d3a848 | 94 | // Returns -1 = error; 0 = success |
YouTahDoug | 1:4a0985d3a848 | 95 | //*********************************************************** |
YouTahDoug | 1:4a0985d3a848 | 96 | int file_rename(const char *oldfname, const char *newfname) { |
YouTahDoug | 1:4a0985d3a848 | 97 | int retval = 0; |
YouTahDoug | 1:4a0985d3a848 | 98 | int ch; |
YouTahDoug | 1:4a0985d3a848 | 99 | |
YouTahDoug | 1:4a0985d3a848 | 100 | FILE *fpold = fopen(oldfname, "r"); // src file |
YouTahDoug | 1:4a0985d3a848 | 101 | FILE *fpnew = fopen(newfname, "w"); // dest file |
YouTahDoug | 1:4a0985d3a848 | 102 | |
YouTahDoug | 1:4a0985d3a848 | 103 | while (1) { // Copy src to dest |
YouTahDoug | 1:4a0985d3a848 | 104 | ch = fgetc(fpold); // until src EOF read. |
YouTahDoug | 1:4a0985d3a848 | 105 | if (ch == EOF) break; |
YouTahDoug | 1:4a0985d3a848 | 106 | fputc(ch, fpnew); |
YouTahDoug | 1:4a0985d3a848 | 107 | } |
YouTahDoug | 1:4a0985d3a848 | 108 | |
YouTahDoug | 1:4a0985d3a848 | 109 | fclose(fpnew); |
YouTahDoug | 1:4a0985d3a848 | 110 | fclose(fpold); |
YouTahDoug | 1:4a0985d3a848 | 111 | |
YouTahDoug | 1:4a0985d3a848 | 112 | fpnew = fopen(newfname, "r"); // Reopen dest to insure |
YouTahDoug | 1:4a0985d3a848 | 113 | if(fpnew == NULL) { // that it was created. |
YouTahDoug | 1:4a0985d3a848 | 114 | retval = (-1); // Return Error. |
YouTahDoug | 1:4a0985d3a848 | 115 | } |
YouTahDoug | 1:4a0985d3a848 | 116 | else { |
YouTahDoug | 1:4a0985d3a848 | 117 | fclose(fpnew); |
YouTahDoug | 1:4a0985d3a848 | 118 | remove(oldfname); // Remove original file. |
YouTahDoug | 1:4a0985d3a848 | 119 | retval = (0); // Return Success. |
YouTahDoug | 1:4a0985d3a848 | 120 | } |
YouTahDoug | 1:4a0985d3a848 | 121 | return (retval); |
YouTahDoug | 1:4a0985d3a848 | 122 | } |
YouTahDoug | 1:4a0985d3a848 | 123 | |
YouTahDoug | 1:4a0985d3a848 | 124 | //*********************************************************** |
YouTahDoug | 1:4a0985d3a848 | 125 | // file_copy: Copies a file |
YouTahDoug | 1:4a0985d3a848 | 126 | // Checks to insure destination file was created. |
YouTahDoug | 1:4a0985d3a848 | 127 | // Returns -1 = error; 0 = success |
YouTahDoug | 1:4a0985d3a848 | 128 | //*********************************************************** |
YouTahDoug | 1:4a0985d3a848 | 129 | int file_copy (const char *src, const char *dst) { |
YouTahDoug | 1:4a0985d3a848 | 130 | int retval = 0; |
YouTahDoug | 1:4a0985d3a848 | 131 | int ch; |
YouTahDoug | 1:4a0985d3a848 | 132 | |
YouTahDoug | 1:4a0985d3a848 | 133 | FILE *fpsrc = fopen(src, "r"); // src file |
YouTahDoug | 1:4a0985d3a848 | 134 | FILE *fpdst = fopen(dst, "w"); // dest file |
YouTahDoug | 1:4a0985d3a848 | 135 | |
YouTahDoug | 1:4a0985d3a848 | 136 | while (1) { // Copy src to dest |
YouTahDoug | 1:4a0985d3a848 | 137 | ch = fgetc(fpsrc); // until src EOF read. |
YouTahDoug | 1:4a0985d3a848 | 138 | if (ch == EOF) break; |
YouTahDoug | 1:4a0985d3a848 | 139 | fputc(ch, fpdst); |
YouTahDoug | 1:4a0985d3a848 | 140 | } |
YouTahDoug | 1:4a0985d3a848 | 141 | fclose(fpsrc); |
YouTahDoug | 1:4a0985d3a848 | 142 | fclose(fpdst); |
YouTahDoug | 1:4a0985d3a848 | 143 | |
YouTahDoug | 1:4a0985d3a848 | 144 | fpdst = fopen(dst, "r"); // Reopen dest to insure |
YouTahDoug | 1:4a0985d3a848 | 145 | if(fpdst == NULL) { // that it was created. |
YouTahDoug | 1:4a0985d3a848 | 146 | retval = (-1); // Return error. |
YouTahDoug | 1:4a0985d3a848 | 147 | } |
YouTahDoug | 1:4a0985d3a848 | 148 | else { |
YouTahDoug | 1:4a0985d3a848 | 149 | fclose(fpdst); |
YouTahDoug | 1:4a0985d3a848 | 150 | retval = (0); // Return success. |
YouTahDoug | 1:4a0985d3a848 | 151 | } |
YouTahDoug | 1:4a0985d3a848 | 152 | return (retval); |
YouTahDoug | 1:4a0985d3a848 | 153 | } |
YouTahDoug | 1:4a0985d3a848 | 154 |