hi

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by mbed official

main.cpp

Committer:
Ishwar_Anjana
Date:
2017-12-25
Revision:
3:5e6d3d2b2557
Parent:
2:e0071aaa133c

File content as of revision 3:5e6d3d2b2557:

#include "mbed.h"
#include "SDFileSystem.h"
#include <string>
#include <vector>\
#include <stdio.h>
#include <iostream>
 
 
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");   
 
    
    
    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);
    }