
untested
Dependencies: SDFileSystem mbed
main.cpp@3:734f4f80832c, 2017-11-10 (annotated)
- Committer:
- nnguyen45
- Date:
- Fri Nov 10 15:00:18 2017 +0000
- Revision:
- 3:734f4f80832c
- Parent:
- 2:1ee5d99a9bae
Working module to parse lesson plan!!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nnguyen45 | 0:276de3654c32 | 1 | #include "mbed.h" |
nnguyen45 | 0:276de3654c32 | 2 | #include <string> |
nnguyen45 | 0:276de3654c32 | 3 | #include <iostream> |
nnguyen45 | 0:276de3654c32 | 4 | using namespace std; |
nnguyen45 | 0:276de3654c32 | 5 | //Emic 2 Hello Speech World Demo |
nnguyen45 | 0:276de3654c32 | 6 | #include "SDFileSystem.h" |
nnguyen45 | 2:1ee5d99a9bae | 7 | Serial pc(USBTX,USBRX); |
nnguyen45 | 0:276de3654c32 | 8 | |
nnguyen45 | 0:276de3654c32 | 9 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
nnguyen45 | 0:276de3654c32 | 10 | |
nnguyen45 | 0:276de3654c32 | 11 | DigitalOut myled(LED1); |
nnguyen45 | 0:276de3654c32 | 12 | |
nnguyen45 | 0:276de3654c32 | 13 | int main() |
nnguyen45 | 0:276de3654c32 | 14 | { |
nnguyen45 | 0:276de3654c32 | 15 | char delimiter = ','; |
nnguyen45 | 0:276de3654c32 | 16 | string letter[2]; |
nnguyen45 | 0:276de3654c32 | 17 | string word[1]; |
nnguyen45 | 0:276de3654c32 | 18 | char check; |
nnguyen45 | 0:276de3654c32 | 19 | string temp; |
nnguyen45 | 3:734f4f80832c | 20 | string tempword = ""; |
nnguyen45 | 0:276de3654c32 | 21 | int counter = 0; |
nnguyen45 | 0:276de3654c32 | 22 | FILE *fp = fopen("/sd/plan.txt", "r"); //create file |
nnguyen45 | 3:734f4f80832c | 23 | if(fp == NULL) { |
nnguyen45 | 3:734f4f80832c | 24 | pc.printf("Could not open file for write\n"); |
nnguyen45 | 3:734f4f80832c | 25 | } |
nnguyen45 | 3:734f4f80832c | 26 | check = fgetc(fp); //grabs a char from file |
nnguyen45 | 3:734f4f80832c | 27 | while(check != '\n') { //while not at the end of line for letters |
nnguyen45 | 3:734f4f80832c | 28 | if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored |
nnguyen45 | 3:734f4f80832c | 29 | letter[counter] = temp; //write letter |
nnguyen45 | 3:734f4f80832c | 30 | pc.printf("Letter: %s \n", letter[counter]); |
nnguyen45 | 3:734f4f80832c | 31 | counter = counter + 1; //increment counter |
nnguyen45 | 3:734f4f80832c | 32 | } else { |
nnguyen45 | 3:734f4f80832c | 33 | temp = check; //store letter |
nnguyen45 | 2:1ee5d99a9bae | 34 | } |
nnguyen45 | 2:1ee5d99a9bae | 35 | check = fgetc(fp); //grabs next char |
nnguyen45 | 2:1ee5d99a9bae | 36 | } |
nnguyen45 | 3:734f4f80832c | 37 | counter = 0; //reset counter |
nnguyen45 | 3:734f4f80832c | 38 | check = fgetc(fp); //grabs next char |
nnguyen45 | 3:734f4f80832c | 39 | while(!feof(fp)) { //while not at the end of line for words |
nnguyen45 | 3:734f4f80832c | 40 | if(check == delimiter) { //when at the comma at the end of a word |
nnguyen45 | 3:734f4f80832c | 41 | word[counter] = tempword; //write word |
nnguyen45 | 3:734f4f80832c | 42 | pc.printf("Word: %s \n", word[counter]); |
nnguyen45 | 3:734f4f80832c | 43 | tempword = ""; |
nnguyen45 | 3:734f4f80832c | 44 | counter = counter + 1; //increment counter |
nnguyen45 | 0:276de3654c32 | 45 | } else { |
nnguyen45 | 3:734f4f80832c | 46 | tempword = tempword + check; //concatenate letters to build word |
nnguyen45 | 0:276de3654c32 | 47 | } |
nnguyen45 | 3:734f4f80832c | 48 | check = fgetc(fp); //grabs next char |
nnguyen45 | 3:734f4f80832c | 49 | } |
nnguyen45 | 0:276de3654c32 | 50 | fclose(fp); //close file |
nnguyen45 | 0:276de3654c32 | 51 | } |