Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:d0eea54553ed
- Child:
- 1:4a0985d3a848
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Jun 21 22:59:02 2011 +0000
@@ -0,0 +1,139 @@
+// 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