Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 4 months ago.
Retrieve File from SD Card
I am using SDFilesystem library for the FRDM-KL46Z to write some data to the SD card as various files with filenames as 1,2,3 so on . How can I retrieve the files or the content so that I can do some string operations over it and then send it over SPI ?
How exactly can I retrieve Data from the SD Card?
2 Answers
9 years, 4 months ago.
The information in the Handbook is very short because you can use the C library . Look at http://www.cplusplus.com/reference/cstdio/ . Hope this help you to start.
9 years, 4 months ago.
Here are some snips from a program I have to read temperature logs from a SD card. This can give you an idea how to start. Each record on the file is separated by ","
This code will not run, there is a lot more behind it. But shows how I extract the data from the SD card which is in the same format.
SD read
#include "mbed.h" // This part code reads back time and scale data for each data file char *pEnd,*p; fp = fopen("/sd/logs.txt", "r"); if(fp == NULL) {SDmenu();} fgets (buf, 128, fp); p = strtok (buf,",");log1time = atoi(p); p = strtok (NULL, ",");log1scale = atoi(p); p = strtok (NULL, ",");log2time = atoi(p); p = strtok (NULL, ",");log2scale = atoi(p); p = strtok (NULL, ",");log3time = atoi(p); p = strtok (NULL, ",");log3scale = atoi(p); p = strtok (NULL, ",");log4time = atoi(p); p = strtok (NULL, ",");log4scale = atoi(p); p = strtok (NULL, ",");log5time = atoi(p); p = strtok (NULL, ",");log5scale = atoi(p); p = strtok (NULL, ",");log6time = atoi(p); p = strtok (NULL, ",");log6scale = atoi(p); p = strtok (NULL, ",");log7time = atoi(p); p = strtok (NULL, ",");log7scale = atoi(p); p = strtok (NULL, ",");log8time = atoi(p); p = strtok (NULL, ",");log8scale = atoi(p); fclose (fp); // This part code reads from a file until it reaches the end. char *pEnd,*p; FILE *fp = fopen("/sd/log1data.txt", "r"); while(fgets (buf, 27, fp) != NULL){ temp = strtof (buf, &pEnd); char* logtime = strtok (buf,","); logtime = strtok (NULL,","); plottemp = 180 - (temp*4); if(xt==0) {xt=plottemp;} oled.drawLine (x,plottemp,x,xt,oled.toRGB(255, 0, 0)); xt=plottemp; x++; oled.setFontSize(FONT12X16);oled.setFontColor(oled.toRGB(255,255,255)); oled.locate (0,0);oled.printf(" %s",logtime); } fclose (fp); // This complete code below gives me all my files and memory used and capacity. void SDfiles(){ oled.setFontColor(oled.toRGB(0,255,128));oled.setFontSize(FONT8X12); float capacity=sd.disk_sectors()*512; oled.locate (5,2);oled.printf("%2.3f Gb Capacity ", capacity/1000000000); oled.locate (0,4);oled.setFontColor(oled.toRGB(255,255,255)); DIR *d = opendir("/sd"); // Opens the root directory of the local file system struct dirent *p; while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system oled.printf(" %s\n", p->d_name); // to stdout. } closedir(d); FILE *fp = fopen("/sd/logs.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp);fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log1data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log2data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log3data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log4data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log5data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log6data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log7data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/log8data.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/maxmin.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/Templogger.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/Templogtime.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); fp = fopen("/sd/system.txt", "r");fseek(fp, 0, SEEK_END);filesize = ftell(fp)+filesize;fseek(fp, 0, SEEK_SET);fclose(fp); oled.setFontSize(FONT8X12);oled.setFontColor(oled.toRGB(0,255,128)); oled.locate (2,18);oled.printf("%3.2f Kb used ",filesize/1000); oled.printf("%d Stored logs",logs); } // This resets the SD card with the files I am using void SDreset() { FILE *fp = fopen("/sd/logs.txt", "w");fclose(fp); fp = fopen("/sd/log1data.txt", "w");fclose(fp); fp = fopen("/sd/log2data.txt", "w");fclose(fp); fp = fopen("/sd/log3data.txt", "w");fclose(fp); fp = fopen("/sd/log4data.txt", "w");fclose(fp); fp = fopen("/sd/log5data.txt", "w");fclose(fp); fp = fopen("/sd/log6data.txt", "w");fclose(fp); fp = fopen("/sd/log7data.txt", "w");fclose(fp); fp = fopen("/sd/log8data.txt", "w");fclose(fp); fp = fopen("/sd/maxmin.txt", "w");fclose(fp); fp = fopen("/sd/Templogger.txt", "w");fclose(fp); fp = fopen("/sd/Templogtime.txt", "w");fclose(fp); fp = fopen("/sd/system.txt", "w");fclose(fp); }