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.
Dependencies: mbed SDFileSystem
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #include "errno.h" 00004 00005 SDFileSystem fs(D11, D12, D13, D7, "sd"); // mosi, miso, sck, cs 00006 FILE* fp; 00007 00008 /** 00009 * @brief 00010 * @note 00011 * @param 00012 * @retval 00013 */ 00014 int main() 00015 { 00016 printf("Mounting file system...\r\n"); 00017 00018 int err = fs.mount(); 00019 printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); 00020 if (err) 00021 return err; 00022 00023 // Open the file. 00024 printf("Opening file '/sd/mytest/sdtest.txt'... "); 00025 fp = fopen("/sd/mytest/sdtest.txt", "w+"); 00026 printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n")); 00027 00028 if (!fp) 00029 { 00030 // Check whether directory '/sd/mytest' exists. 00031 printf("\r\nChecking directory '/sd/mytest'...\r\n"); 00032 00033 struct stat info; 00034 err = stat("/sd/mytest", &info); 00035 if (err) 00036 { 00037 printf("Directory '/sd/mytest' does not exist.\r\n"); 00038 printf("Trying to create it..."); 00039 err = mkdir("/sd/mytest", 0777); 00040 printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); 00041 if (err) 00042 return err; 00043 } 00044 00045 // Create a new 'sdtest.txt' file. 00046 printf("File not found, creating a new one...\r\n"); 00047 fp = fopen("/sd/mytest/sdtest.txt", "w+"); 00048 printf("%s\r\n", (!fp ? "Failed :(" : "OK")); 00049 if (!fp) 00050 { 00051 error("error: %s (%d)\r\n", strerror(errno), -errno); 00052 return errno; 00053 } 00054 } 00055 00056 for (int i = 0; i < 10; i++) 00057 { 00058 printf("Writing numbers (%d/%d)... ", i, 10); 00059 err = fprintf(fp, " %d\r\n", i); 00060 if (err < 0) 00061 { 00062 printf("Fail :(\r\n"); 00063 error("error: %s (%d)\r\n", strerror(errno), -errno); 00064 } 00065 else 00066 printf("OK\r\n"); 00067 } 00068 00069 printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10); 00070 err = fclose(fp); 00071 printf("Closing file '/sd/mytest/sdtest.txt'... "); 00072 printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n")); 00073 if (err) 00074 return err; 00075 00076 return 0; 00077 }
Generated on Thu Aug 4 2022 02:59:11 by
1.7.2