Emery Premeaux / Mbed 2 deprecated Examples

Dependencies:   mbed SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SD_Index_File.cpp Source File

SD_Index_File.cpp

00001 /*
00002  * Opens index.txt, reads a number from the file (as charactor binary value)
00003  * Closes that file, then creates a file name string by adding one to the index number
00004  * Creates and opens a new file with the file name string, and writes some text before closing.
00005  *
00006  */ 
00007 
00008 
00009 #ifdef COMPILE_SD_Index_File
00010 
00011 #include "SDFileSystem.h"
00012 
00013 SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd");     // MOSI, MISO SCLK, CS, "name"
00014 
00015 int last_file_num;
00016 
00017 
00018 int main() {
00019     FILE *IndexFile = fopen("/sd/index.txt", "r"); // get the last file number from the index file
00020     last_file_num = fgetc(IndexFile);
00021     printf("Got: %i \n", last_file_num);
00022     fclose(IndexFile);
00023     last_file_num++;
00024     IndexFile = fopen("/sd/index.txt", "w"); // wrte the new number into the index file
00025     fputc(last_file_num, IndexFile);
00026     fclose(IndexFile);
00027     
00028     char filename[12];
00029     sprintf(filename, "/sd/%i.txt", last_file_num);   // build new filename to open
00030 
00031     FILE *File = fopen(filename, "w");
00032     if(File == NULL) {
00033         printf("Could not open file to write\n");
00034     }
00035     else {
00036         printf("SD card file opened: %s\n", filename);
00037         fprintf(File, "heres some data!");
00038         fclose(File);
00039         printf("closed\n");
00040     }
00041 }
00042 
00043 #endif
00044