untested

Dependencies:   SDFileSystem mbed

main.cpp

Committer:
nnguyen45
Date:
2017-11-10
Revision:
3:734f4f80832c
Parent:
2:1ee5d99a9bae

File content as of revision 3:734f4f80832c:

#include "mbed.h"
#include <string>
#include <iostream>
using namespace std;
//Emic 2 Hello Speech World Demo
#include "SDFileSystem.h"
Serial pc(USBTX,USBRX);

SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

DigitalOut myled(LED1);

int main()
{
    char delimiter = ',';
    string letter[2];
    string word[1];
    char check;
    string temp;
    string tempword = "";
    int counter = 0;
    FILE *fp = fopen("/sd/plan.txt", "r"); //create file
    if(fp == NULL) {
        pc.printf("Could not open file for write\n");
    }
    check = fgetc(fp); //grabs a char from file
    while(check != '\n') {  //while not at the end of line for letters
        if((check == delimiter) && (temp.length() == 1)) {  //at a comma and have a letter stored
            letter[counter] = temp; //write letter
            pc.printf("Letter: %s \n", letter[counter]);
            counter = counter + 1;  //increment counter
        } else {
            temp = check;   //store letter
        }
        check = fgetc(fp);  //grabs next char
    }
    counter = 0;    //reset counter
    check = fgetc(fp);  //grabs next char
    while(!feof(fp)) {  //while not at the end of line for words
        if(check == delimiter) {  //when at the comma at the end of a word
            word[counter] = tempword;   //write word
            pc.printf("Word: %s \n", word[counter]);
            tempword = "";
            counter = counter + 1;  //increment counter
        } else {
            tempword = tempword + check;    //concatenate letters to build word
        }
        check = fgetc(fp);  //grabs next char
    }
    fclose(fp); //close file
}