Two utility functions to read/write small files

This commit is contained in:
Stefan `Sec` Zehl 2011-08-01 01:12:58 +02:00
parent 5d0887be71
commit 6299286348
2 changed files with 40 additions and 0 deletions

View File

@ -332,6 +332,8 @@ int ff_del_syncobj (_SYNC_t); /* Delete a sync object */
#define FILENAMELEN 13 // 8+1+3+1
const char* f_get_rc_string (FRESULT rc);
void fsInit();
int readFile(char * filename, char * data, int len);
int writeFile(char * filename, char * data, int len);
#ifdef __cplusplus
}

View File

@ -22,3 +22,41 @@ const char* f_get_rc_string (FRESULT rc) {
void fsInit(){
f_mount(0, &FatFs);
};
int readFile(char * filename, char * data, int len){
FIL file;
UINT readbytes;
int res;
res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ);
if(res){
return -1;
};
res = f_read(&file, data, len-1, &readbytes);
if(res){
return -1;
};
data[readbytes]=0;
return readbytes;
};
int writeFile(char * filename, char * data, int len){
FIL file;
UINT writebytes;
int res;
res=f_open(&file, filename, FA_OPEN_ALWAYS|FA_WRITE);
if(res){
return -1;
};
res = f_write(&file, data, len, &writebytes);
if(res){
return -1;
};
return writebytes;
};