hi
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
Diff: main.cpp
- Revision:
- 2:e0071aaa133c
- Parent:
- 0:bdbd3d6fc5d5
--- a/main.cpp Tue May 16 05:18:55 2017 +0000 +++ b/main.cpp Mon Dec 25 17:35:11 2017 +0000 @@ -1,19 +1,139 @@ #include "mbed.h" #include "SDFileSystem.h" +#include <string> +#include <vector>\ +#include <stdio.h> +#include <iostream> -SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board + +SDFileSystem sd(D11, D12, D13, D10, "sd"); // the pinout on the mbed Cool Components workshop board + +//.....assumes SDFileSystem is setup in earlier code for device "/sd" + +vector<string> filenames; //filenames are stored in a vector string +vector<string> filedata; //file data is stored in a vector string +/////////////////////////////////////////////////////////////////////////////// +using namespace std; +typedef unsigned char BYTE; + +long getFileSize(FILE *file) +{ + long lCurPos,lEndPos; + lCurPos = ftell(file); + fseek(file,lCurPos, 0); + return lEndPos; +} +////////////////////////////////////////////////////////////////////////////// + + +void read_file_names(char *dir) +{ + DIR *dp; + struct dirent *dirp; + dp = opendir(dir); + //read all directory and file names in current directory into filename vector + while((dirp = readdir(dp)) != NULL) { + filenames.push_back(string(dirp->d_name)); + } + closedir(dp); +} +void read_file_data (char *dir) +{ + DIR *dp; + struct dirent *dirp; + dp = opendir(dir); + //read all directory and file names in current directory into filename vector + while((dirp = readdir(dp)) != NULL) { + filenames.push_back(string(dirp->d_name)); + } + closedir(dp); +} int main() { printf("Hello World!\n"); - mkdir("/sd/mydir", 0777); + - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); + FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } fprintf(fp, "Hello fun SD Card World!"); + fclose(fp); printf("Goodbye World!\n"); -} + + printf("Reading files!\n"); + + read_file_names("/sd"); + //read_file_data("/sd/sky.raw"); + + // print filename strings from vector using an iterator + for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) + { + printf("%s\n\r",(*it).c_str()); + } + ////////////////////////////////////////////////////////////////////////////// + const char *filePath = "/sd/sky.raw"; +BYTE *fileBuf; +FILE *file = NULL; + +if ((file = fopen(filePath, "rb"))== NULL) + { + //cout << "Could not open the file" << end1 ; + printf("Could not open the file"); + + } +else +{ + + // cout << "opening file successfull" << end1 ; + printf("opening file successfull"); + } + + long fileSize = getFileSize(file); + + fileBuf = new BYTE[fileSize]; + + fread(fileBuf, fileSize, 1, file); + + for(int i=0; i<100; i++) + { + printf("%X", fileBuf[i]); + } + +cin.get(); +delete[]fileBuf; +fclose(file); +///////////////////////////////////////////////////////////////////////////////// + + + +///////////////////////////////////////////////////////////////////////////////// + + + + fp = fopen("/sd/mydir/sdtest.txt", "r+"); + if(fp == NULL){ + printf("error in opening file"); + } + +// fread(fp, buff, 10, readBuff); + char line[100]; /* Line buffer */ +// FRESULT fr; /* FatFs return code */ + /* Read all lines and display it */ + while (fgets(line, sizeof line, fp)) { + printf(line); + } + + fscanf(fp, line, sizeof line); + + printf("Reading line %s", line); + } + + + + + +