Example of reading and writing text file in microSD card for DISCO-F746. DISCO-F746 で microSD カードのテキスト・ファイルの読み書きを行う例.

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Revision:
0:3e46577dc273
Child:
1:7aa80a497ed2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Oct 09 10:11:14 2016 +0000
@@ -0,0 +1,60 @@
+//--------------------------------------------------------------
+//  SD カードのテキスト・ファイルの読み書きの例
+//
+//  mount(), unmount() を使うことを除けば通常のテキストファイルの
+//  読み書きと同じ.
+//
+//  2016/10/09, Copyright (c) 2016 MIKAMI, Naoki
+//--------------------------------------------------------------
+
+#include "mbed.h"
+#include "SDFileSystem.h"   // SDFileSystem クラスを使うため
+
+DigitalOut led_green(LED1);
+SDFileSystem sd("sd");      // SDFileSystem: SD 用のクラス, 引数の文字列は任意
+
+int main()
+{
+    printf("\r\nSD Text file write/read test: started\r\n\n");
+
+    sd.mount(); // SD 用
+
+    // SD へ出力
+    FILE *fp = fopen("/sd/WriteReadTest.txt", "w");
+    if (fp == NULL)
+    {
+        fprintf(stderr, "Open error for writing!!\r\n");
+        while (true) {}
+    }
+
+    fprintf(fp, "Hello!\n");
+    fprintf(fp, "Example of writing and reading of text file.\n");
+    fclose(fp);
+
+    // SD から入力
+    fp = fopen("/sd/TestWrite.txt", "r");
+    if (fp == NULL)
+    {
+        fprintf(stderr, "Open error for reading!!\r\n");
+        while (true) {}
+    }
+
+    while (true)
+    {
+        int chr = fgetc(fp);
+        if (chr == EOF) break;
+        printf("%c", chr);
+        if (chr == '\n') printf("\r");
+    }
+    printf("\r\n");
+    
+    fclose(fp);
+   
+    sd.unmount();    // SD 用
+    
+    while(true)
+    {
+        led_green = !led_green;
+        wait(0.2f);
+    }
+}