Read/Write the SD card(using SSP)
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
Diff: main.cpp
- Revision:
- 4:68cb56ba60c6
- Parent:
- 3:017b99069995
--- a/main.cpp Mon Jun 29 03:38:40 2015 +0000 +++ b/main.cpp Thu Jul 02 07:37:06 2015 +0000 @@ -10,24 +10,34 @@ #define MAX_SIZE 100 SDFileSystem sd(MOSI, MISO, CLK, SEL, "sd"); // the pinout on the mbed Cool Components workshop board -char sdread[MAX_SIZE]; + int main() { - mkdir("/sd/mydir", 0777); - - 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!"); - fclose(fp); - - printf("Reading from SD card...\r\n"); - fp = fopen("/sd/mydir/sdtest.txt", "r"); - if (fp != NULL) { - fgets(sdread,MAX_SIZE,fp); - printf("%s", sdread); - fclose(fp); - } else { - printf("failed!\n"); - } + + char str[MAX_SIZE]; + mkdir("/sd/mydir", 0777); + //"w" : {Write) - Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file. + //"a+" :(append/update) - open (or create if the file does not exist) for update at the end of the file + 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("Insert data:"); + scanf("%s",str); + printf("\r\n"); + fflush(stdin); + fprintf(fp,"%s", str); + fclose(fp); + + printf("Reading from SD card...\r\n"); + fp = fopen("/sd/mydir/sdtest.txt", "r"); + if (fp != NULL) { + fgets(str,MAX_SIZE,fp); + printf("%s", str); + printf("\r\n"); + fclose(fp); + } else { + printf("failed!\n"); + } + }