-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDcard.c
31 lines (27 loc) · 878 Bytes
/
SDcard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "cfs/cfs.h"
int file_read(char* filename, char* storage, unsigned int max_len, unsigned int offset)
{
// Obtain a file descriptor for the file, capable of
// handling both reads and writes.
int fd = cfs_open(filename, CFS_READ);
if (fd < 0) {
printf("Failed to open %s\n", filename);
return -1;
}
//printf("Opened file %s\n", filename);
// Read the message back
unsigned int file_len = cfs_seek(fd, offset, CFS_SEEK_END);
file_len = file_len<max_len?file_len:max_len;
cfs_seek(fd, offset, CFS_SEEK_SET);
unsigned int r = cfs_read(fd, storage, file_len);
if (r < file_len) {
printf("Failed to read %d bytes from %s, got %d bytes\n",
file_len, filename, r);
cfs_close(fd);
return 0;
}
storage[r] = '\0';
// Close the file and release resources associated with fd
cfs_close(fd);
return file_len;
}