-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPIFFS-Config.cpp
72 lines (51 loc) · 1.75 KB
/
SPIFFS-Config.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <LoopSpeedLogger.h>
#include <stdlib.h>
#include "FS.h"
#include "SPIFFS.h"
#define FORMAT_SPIFFS_IF_FAILED true
String readFile(const char *path) {
String buffer = "";
File file = SPIFFS.open(path);
if (!file || file.isDirectory())
return "";
while (file.available())
buffer += (char)file.read();
return buffer;
}
bool writeFile(const char *path, const char *message) {
File file = SPIFFS.open(path, FILE_WRITE);
return file.print(message);
}
bool appendFile(const char *path, const char *message) {
File file = SPIFFS.open(path, FILE_APPEND);
return file.print(message);
}
void setup() {
Serial.begin(115200);
// mount SPIFFS
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
Serial.println("SPIFFS Mount Failed");
return;
}
delay(2000);
// write to a file, the function returns false is failed
writeFile("/myfile", "foo");
// read from the file, returns empty string if failed
Serial.println(readFile("/myfile"));
// append to the file, returns false is failed
appendFile("/myfile", "\nbar");
// read from the file, returns empty string if failed
Serial.println(readFile("/myfile"));
// check if the file exists
Serial.println(SPIFFS.exists("/myfile") ? "the file exists" : "file does not exist");
// delete the file, returns false is failed
SPIFFS.remove("/myfile");
//check if the file exists
Serial.println(SPIFFS.exists("/myfile") ? "the file exists" : "file does not exist");
// it can also be done with pure bytes, but the array has to end with 0 because SPIFFS handles everything as a string
const char a[] = {65, 66, 67, 0};
writeFile("/byte", a);
Serial.println(readFile("/byte"));
}
void loop() {
}