SD Hello world using usb virtual com port

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by mbed official

main.cpp

Committer:
kzar
Date:
2018-10-16
Revision:
2:b86f4a4aa079
Parent:
0:bdbd3d6fc5d5

File content as of revision 2:b86f4a4aa079:

#include "mbed.h"
#include "SDFileSystem.h"

Serial pc(USBTX, USBRX);
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
 
int main() {
    //Write to the file and close it 
    mkdir("/sd/mydir", 0777);
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
        pc.printf("fucccccckk");
    }
    fprintf(fp, "Hello SD World!");
    fclose(fp);
    
    //Open the file and read out the data to the pc
    fp = fopen("/sd/mydir/sdtest.txt", "r");
    char Buffer[256];
 
    if(fp == NULL) { error("Could not open file for reading\r\n"); }
    
    while(fgets (Buffer, 256, fp) != NULL){
        Buffer[strlen(Buffer)-1] = 0;
        pc.printf("\"%s\" \r\n", Buffer);
     
    }
    
    fclose(fp);
}