The program will open a file called test.txt in the root of the SD card, and will create one if it does not exist. It will then write "one two three four five" in the .txt file. It will then read the text and output the result. You will need a terminal application (I recommend PuTTy) in order to see the outputs. The current program overwrites anything that was previous on the SD card. To prevent this, change the "w" to "a" during the writing process. This changes the instruction from a 'write' to an 'append'.
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
Revision 1:2cf8f0893afd, committed 2014-07-24
- Comitter:
- bentrevett
- Date:
- Thu Jul 24 12:55:11 2014 +0000
- Parent:
- 0:bdbd3d6fc5d5
- Commit message:
- Reading and Writing to SD Card; ; Description:; ; A small project made with mbed (mbed.org) on the FRDM-KL64 using the SD card capabilities.; The program will open a file called test.txt in the root of the SD card, and will create one if it does not exi
Changed in this revision
--- a/SDFileSystem.lib Fri Dec 07 11:25:01 2012 +0000 +++ b/SDFileSystem.lib Thu Jul 24 12:55:11 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4 +http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- a/main.cpp Fri Dec 07 11:25:01 2012 +0000 +++ b/main.cpp Thu Jul 24 12:55:11 2014 +0000 @@ -1,19 +1,42 @@ #include "mbed.h" #include "SDFileSystem.h" -SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board - +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //mosi, miso, sclk, cs + + +#define charlimit 100 +char words[charlimit]; +int n=0,c; + int main() { - printf("Hello World!\n"); - - mkdir("/sd/mydir", 0777); + + //writing to SD card + printf("Opening SD card...\r\n"); + + FILE *fp = fopen("/sd/test.txt", "w"); + if(fp == NULL) { + error("Could not open file for write!\r\n"); + } - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); - if(fp == NULL) { - error("Could not open file for write\n"); - } - fprintf(fp, "Hello fun SD Card World!"); + printf("Writing to SD card...\r\n"); + fprintf(fp, "one two three four five\r\n"); fclose(fp); + + //reading from SD card + FILE *fp1 =fopen("/sd/test.txt.", "r"); + if(fp1==NULL){ + error("Could not open file for read!\r\n"); + } + + printf("Reading from SD card...\r\n"); + + while((c=fgetc(fp1)) && c!=EOF){ + words[n]=c; + n++; + } + + printf("Read from SD card: %s",words); + + fclose(fp1); - printf("Goodbye World!\n"); }
--- a/mbed.bld Fri Dec 07 11:25:01 2012 +0000 +++ b/mbed.bld Thu Jul 24 12:55:11 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/63cdd78b2dc1 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae \ No newline at end of file