Opens/Creates txt file, writes to it, then prints to terminal from it
Dependencies: SDFileSystem mbed
Fork of FTF2014_lab4 by
Revision 3:deefd66fdc9e, committed 2015-03-04
- Comitter:
- joshwilkins2013
- Date:
- Wed Mar 04 00:32:29 2015 +0000
- Parent:
- 2:41dd6f412728
- Commit message:
- Simple SD card test outputing floats in a loop
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Apr 07 19:36:26 2014 +0000 +++ b/main.cpp Wed Mar 04 00:32:29 2015 +0000 @@ -2,37 +2,35 @@ #include "SDFileSystem.h" SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS + Serial pc(USBTX, USBRX); -FILE *fp; -char buffer[1024]; + +FILE *Data; // Creates name of file +char buffer[1024]; // Buffer size (# of characters) int main() { pc.printf("Initializing \n"); - wait(2); - - fp = fopen("/sd/hello.txt", "r"); - if (fp != NULL) { - fclose(fp); - remove("/sd/hello.txt"); - pc.printf("Remove an existing file with the same name \n"); + + float x = 214; + double y = 200; + + Data = fopen("/sd/Data.txt", "r"); // Opens file if it exists, r means read + if (Data != NULL) remove("/sd/Data.txt"); // If it does close it then remove it + fclose(Data); + + Data = fopen("/sd/Data.txt", "w"); // w means write + for(int i=0; i<4; i++){ + if (Data == NULL) pc.printf("Unable to write the file \n\r"); // This chunk for writing to file on SD card + else fprintf(Data, "%f\t%f\n",x,y); // can read float or double not int (only checked these three types) + x++; + y--; } - - printf("\nWriting data to the sd card \n"); - fp = fopen("/sd/hello.txt", "w"); - if (fp == NULL) { - pc.printf("Unable to write the file \n"); - } else { - fprintf(fp, "mbed SDCard application!"); - fclose(fp); - pc.printf("File successfully written! \n"); + fclose(Data); + + Data = fopen("/sd/Data.txt", "r"); + if (Data != NULL) { + int size = fread(buffer, sizeof(char), 1024, Data); // This chunk for reading the file on the SD card + printf("%s\n", buffer); // can read float or double not int (only checked these three types) } - - printf("\nReading data from the SD card. \n"); - fp = fopen("/sd/hello.txt", "r"); - if (fp != NULL) { - int size = fread(buffer, sizeof(char), 1024, fp); - printf("Number of data read: %d, text from hello.txt file: %s \n", size, buffer); - fclose(fp); - } - printf("End of Lab 4. \n"); + fclose(Data); }