Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
Fork of 4180_Lab2_SD by
main.cpp@1:d2b9f2f42fe5, 2016-09-25 (annotated)
- Committer:
- jeremycai3721
- Date:
- Sun Sep 25 19:03:25 2016 +0000
- Revision:
- 1:d2b9f2f42fe5
- Parent:
- 0:bdbd3d6fc5d5
CodeShare
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
| mbed_official | 0:bdbd3d6fc5d5 | 2 | #include "SDFileSystem.h" |
| mbed_official | 0:bdbd3d6fc5d5 | 3 | |
| mbed_official | 0:bdbd3d6fc5d5 | 4 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
| mbed_official | 0:bdbd3d6fc5d5 | 5 | |
| mbed_official | 0:bdbd3d6fc5d5 | 6 | int main() { |
| mbed_official | 0:bdbd3d6fc5d5 | 7 | printf("Hello World!\n"); |
| mbed_official | 0:bdbd3d6fc5d5 | 8 | |
| mbed_official | 0:bdbd3d6fc5d5 | 9 | mkdir("/sd/mydir", 0777); |
| mbed_official | 0:bdbd3d6fc5d5 | 10 | |
| mbed_official | 0:bdbd3d6fc5d5 | 11 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
| mbed_official | 0:bdbd3d6fc5d5 | 12 | if(fp == NULL) { |
| mbed_official | 0:bdbd3d6fc5d5 | 13 | error("Could not open file for write\n"); |
| mbed_official | 0:bdbd3d6fc5d5 | 14 | } |
| mbed_official | 0:bdbd3d6fc5d5 | 15 | fprintf(fp, "Hello fun SD Card World!"); |
| mbed_official | 0:bdbd3d6fc5d5 | 16 | fclose(fp); |
| mbed_official | 0:bdbd3d6fc5d5 | 17 | |
| mbed_official | 0:bdbd3d6fc5d5 | 18 | printf("Goodbye World!\n"); |
| jeremycai3721 | 1:d2b9f2f42fe5 | 19 | |
| jeremycai3721 | 1:d2b9f2f42fe5 | 20 | |
| jeremycai3721 | 1:d2b9f2f42fe5 | 21 | // example of reading a file one byte at a time |
| jeremycai3721 | 1:d2b9f2f42fe5 | 22 | // and display it in hex format on the terminal |
| jeremycai3721 | 1:d2b9f2f42fe5 | 23 | |
| jeremycai3721 | 1:d2b9f2f42fe5 | 24 | unsigned char c; // a single byte buffer |
| jeremycai3721 | 1:d2b9f2f42fe5 | 25 | |
| jeremycai3721 | 1:d2b9f2f42fe5 | 26 | fp = fopen("/sd/mydir/sdtest.txt", "r"); // open the file in 'read' mode |
| jeremycai3721 | 1:d2b9f2f42fe5 | 27 | |
| jeremycai3721 | 1:d2b9f2f42fe5 | 28 | while (!feof(fp)){ // while not end of file |
| jeremycai3721 | 1:d2b9f2f42fe5 | 29 | c=fgetc(fp); // get a character/byte from the file |
| jeremycai3721 | 1:d2b9f2f42fe5 | 30 | printf("%c",c); // |
| jeremycai3721 | 1:d2b9f2f42fe5 | 31 | } |
| jeremycai3721 | 1:d2b9f2f42fe5 | 32 | printf("\n\r"); |
| jeremycai3721 | 1:d2b9f2f42fe5 | 33 | fclose(fp); // close the file |
| jeremycai3721 | 1:d2b9f2f42fe5 | 34 | |
| mbed_official | 0:bdbd3d6fc5d5 | 35 | } |
