Example of using the SDFileSystem library on a K64F to write data to files and also read data into dynamically created arrays.
Diff: main.cpp
- Revision:
- 1:caceb9d9d17a
- Parent:
- 0:5448330e1a33
--- a/main.cpp Fri Mar 11 16:07:41 2016 +0000 +++ b/main.cpp Mon Feb 03 12:06:21 2020 +0000 @@ -15,14 +15,12 @@ // Connections to SD card holder on K64F (SPI interface) SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS -Serial serial(USBTX, USBRX); // for PC debug void delete_file(char filename[]); int main() { - serial.baud(115200); // full-speed! - serial.printf("#### SD Card Example #####\n"); + printf("#### SD Card Example #####\n"); FILE *fp; // this is our file pointer wait(1); @@ -43,11 +41,11 @@ int top_score = 56; // random example if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); } else { // opened file so can write - serial.printf("Writing to file...."); + printf("Writing to file...."); fprintf(fp, "%d",top_score); // ensure data type matches - serial.printf("Done.\n"); + printf("Done.\n"); fclose(fp); // ensure you close the file after writing } @@ -58,10 +56,10 @@ int stored_top_score = -1; // -1 to demonstrate it has changed after reading if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); } else { // opened file so can write fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) - serial.printf("Read %d from file.\n",stored_top_score); + printf("Read %d from file.\n",stored_top_score); fclose(fp); // ensure you close the file after reading } @@ -73,14 +71,14 @@ fp = fopen("/sd/test.txt", "a"); if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); } else { // opened file so can write - serial.printf("Writing to file...."); + printf("Writing to file...."); for(int i = 1; i <= 50; i++) { float dummy = 1000.0F/i; // dummy variable fprintf(fp, "%d,%f\n",i,dummy); // print formatted string to file (CSV) } - serial.printf("Done.\n"); + printf("Done.\n"); fclose(fp); // ensure you close the file after writing } @@ -93,9 +91,9 @@ // now open file for reading...note the 'r' fp = fopen("/sd/test.txt", "r"); if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); } else { - serial.printf("Reading file....\n"); + printf("Reading file....\n"); int i; // create suitable variables to store the data in the file float value; @@ -104,9 +102,9 @@ // to the variables. Also the format of the string must match what // is in the file while (fscanf(fp, "%d,%f", &i, &value) != EOF) { - serial.printf("%d,%f\n",i,value); + printf("%d,%f\n",i,value); } - serial.printf("Done.\n"); + printf("Done.\n"); fclose(fp); // ensure you close the file after reading } @@ -123,9 +121,9 @@ float *value_array; // note memory will be in heap rather than on the stack if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); + printf("Error! Unable to open file!\n"); } else { - serial.printf("Counting lines in file....\n"); + printf("Counting lines in file....\n"); //Since we may not know the // number of lines in the files ahead of time, we'll first count them // * means scan but don't save @@ -134,8 +132,8 @@ } - serial.printf("Read %d lines\n",n); - serial.printf("Creating dynamic arrays...\n"); + printf("Read %d lines\n",n); + printf("Creating dynamic arrays...\n"); // calloc creates an array and initilises to 0 // malloc returns unitialised array - diffrent syntax index_array = (int *)calloc(n, sizeof (int)); @@ -143,31 +141,31 @@ int i=0; rewind(fp); // 'scrolled' to end of file, so go back to beginning - serial.printf("Reading into arrays...\n"); + printf("Reading into arrays...\n"); while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) { i++; // read data into array and increment index } - serial.printf("Done.\n"); + printf("Done.\n"); fclose(fp); // ensure you close the file after reading } // we should now have the data in the arrays, will print to serial to check for(int i=0; i<n ; i++) { - serial.printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); + printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); } /////////////////////////////////////////////////// - serial.printf("End of SD card example\n"); + printf("End of SD card example\n"); } void delete_file(char filename[]) { - serial.printf("Deleting file '%s'...",filename); + printf("Deleting file '%s'...",filename); FILE *fp = fopen(filename, "r"); // try and open file if (fp != NULL) { // if it does open... fclose(fp); // close it remove(filename); // and then delete - serial.printf("Done!\n"); + printf("Done!\n"); } // if we can't open it, it doesn't exist and so we can't delete it }