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.
Revision 1:4a0985d3a848, committed 2011-06-22
- Comitter:
- YouTahDoug
- Date:
- Wed Jun 22 16:52:20 2011 +0000
- Parent:
- 0:d0eea54553ed
- Commit message:
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r d0eea54553ed -r 4a0985d3a848 main.cpp --- a/main.cpp Tue Jun 21 22:59:02 2011 +0000 +++ b/main.cpp Wed Jun 22 16:52:20 2011 +0000 @@ -1,139 +1,154 @@ -// example writing to SD card, sford -// Modified 21 June 2011 by D. Wendelboe to test SD file and directory creation. -// Two procedures were added. One to renam a file (via copy), and a procedure to -// copy a file. - -#include "mbed.h" -#include "SDFileSystem.h" - -SDFileSystem sd(p5, p6, p7, p8, "sd"); - -int file_rename(const char *oldfile, const char *newfile); -int file_copy(const char *oldfile, const char *newfile); - -int main() -{ - int i, status; - - printf("Hello World!\n"); - - mkdir("/sd/mydir1", 0777); - mkdir("/sd/mydir2", 0777); - mkdir("/sd/mydir3", 0777); - -//-------------------------------------------------------------- - FILE *fp = fopen("/sd/mydir1/sdtest.txt", "w"); - if(fp == NULL) { - error("Could not open file for write\n"); - } - fprintf(fp, "Hello SD Card World!\n"); - for (i=0; i<10; i++) - { - fprintf(fp, "Line #%i\n", i); - } - fclose(fp); - -//-------------------------------------------------------------- - printf("Open directory on /sd\n"); - DIR *d = opendir("/sd"); - struct dirent *p; - while((p = readdir(d)) != NULL) { - printf("%s\n", p->d_name); - } - closedir(d); - printf("Directory closed\n"); - - //-------------------------------------------------------------- - printf("Remove /sd/mydir3\n"); - remove("/sd/mydir3"); - - //-------------------------------------------------------------- - printf("Rename sdtest.txt to doug.txt\n"); - status = file_rename("/sd/mydir1/sdtest.txt", "/sd/mydir1/doug.txt"); - - if (status == -1) printf("Error: New file not created: %d\n", status); - else printf("New file was created: %d\n", status); - -//-------------------------------------------------------------- - status = file_copy("/sd/mydir1/doug.txt", "/sd/mydir1/doug2.txt"); - - if (status == -1) printf("Error: file not copied: %d\n", status); - else printf("Success: file copied: %d\n", status); - -//-------------------------------------------------------------- - fp = fopen("/sd/mydir2/sdtest2.txt", "w"); - if(fp == NULL) { - error("Could not open file for write\n"); - } - fprintf(fp, "This is a second directory.\n"); - for (i=0; i<10; i++) { - fprintf(fp, "Line #%C\n", i+'a'); - } - fclose(fp); - - printf("Goodbye World!\n"); -} - -//*********************************************************** -// file_rename: renames a file. -// Kind of hoakey as it moves data instead of adjusting -// the file name in the directory. -// Checks to insure the file was renamed. -// Returns -1 = error; 0 = success -//*********************************************************** -int file_rename(const char *oldfile, const char *newfile) { - int retval = 0; - int ch; - - FILE *fpold = fopen(oldfile, "r"); - FILE *fpnew = fopen(newfile, "w"); - - while (1) { - ch = fgetc(fpold); - if (ch == EOF) break; - fputc(ch, fpnew); - } - - fclose(fpnew); - fclose(fpold); - - fpnew = fopen(newfile, "r"); - if(fpnew == NULL) { - retval = (-1); - } - else { - remove(oldfile); - } - return (retval); -} - -//*********************************************************** -// file_copy: Copies a file -// Checks to insure destination file was created. -// Returns -1 = error; 0 = success -//*********************************************************** -int file_copy(const char *oldfile, const char *newfile) { - int retval = 0; - int ch; - - FILE *fpold = fopen(oldfile, "r"); - FILE *fpnew = fopen(newfile, "w"); - - while (1) { - ch = fgetc(fpold); - if (ch == EOF) break; - fputc(ch, fpnew); - } - - fclose(fpnew); - fclose(fpold); - - fclose(fpnew); - fclose(fpold); - - fpnew = fopen(newfile, "r"); - if(fpnew == NULL) { - retval = (-1); - } - return (retval); -} \ No newline at end of file +//***************************************************************************** +// Example writing to SD card, sford +// +// 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. +//***************************************************************************** + +#include "mbed.h" +#include "SDFileSystem.h" + +#define PC_BAUD 38400 + +SDFileSystem sd(p5, p6, p7, p8, "sd"); +Serial pc(USBTX,USBRX); + +int file_rename(const char *oldfname, const char *newfname); +int file_copy(const char *src, const char *dst); + +int main() +{ + int i, status; + pc.baud(PC_BAUD); + + printf("\nExercise SD Card functions\n"); + + printf("Create 3 directories on SD Card.\n"); + mkdir("/sd/mydir1", 0777); + mkdir("/sd/mydir2", 0777); + mkdir("/sd/mydir3", 0777); + +//-------------------------------------------------------------- + printf("Create \"/sd/mydir1/sdtest.txt\" and write some data in it\n"); + + FILE *fp = fopen("/sd/mydir1/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + fprintf(fp, "Hello SD Card World!\n"); + for (i=0; i<10; i++) { + fprintf(fp, "Line #%i\n", i); + } + fclose(fp); + +//-------------------------------------------------------------- + printf("Open directory on /sd\n"); + DIR *d = opendir("/sd"); + struct dirent *p; + while((p = readdir(d)) != NULL) { + printf("%s\n", p->d_name); + } + closedir(d); + printf("Directory closed\n"); + + //-------------------------------------------------------------- + printf("Remove /sd/mydir3\n"); + remove("/sd/mydir3"); + +//-------------------------------------------------------------- + printf("List directory again. mydir3 should be gone.\n"); + d = opendir("/sd"); + while((p = readdir(d)) != NULL) { + printf("%s\n", p->d_name); + } + closedir(d); + + //-------------------------------------------------------------- + printf("Rename \"/sd/mydir1/sdtest.txt\" to \"/sd/mydir1/new_name.txt\"\n"); + status = file_rename("/sd/mydir1/sdtest.txt", "/sd/mydir1/new_name.txt"); + + if (status == -1) printf("Error: file not renamed. (status=%d)\n", status); + else printf("Success: file renamed OK (status=%d)\n", status); + +//-------------------------------------------------------------- + printf("Copy \"/sd/mydir1/doug.txt\" to \"/sd/mydir1/new_copy.txt\"\n"); + status = file_copy("/sd/mydir1/new_name.txt", "/sd/mydir1/new_copy.txt"); + + if (status == -1) printf("Error: file not copied. (status=%d)\n", status); + else printf("Success: file copied OK (status=%d)\n", status); + +//-------------------------------------------------------------- + printf("Done.\n"); +} + +//*********************************************************** +// 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); +} +