Skip to content

Commit 042d105

Browse files
committed
File system API reference
1 parent e836e7c commit 042d105

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

hardware/esp8266com/esp8266/doc/reference.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,145 @@ const char HTTP[] PROGMEM = "http:";
103103
}
104104
```
105105

106+
## File system
107+
108+
### File system object (SPIFFS)
109+
110+
#### begin
111+
112+
```c++
113+
SPIFFS.begin()
114+
```
115+
116+
This method mounts SPIFFS file system. It must be called before any other
117+
FS APIs are used. Returns *true* if file system was mounted successfully, false
118+
otherwise.
119+
120+
#### open
121+
122+
```c++
123+
SPIFFS.open(path, mode)
124+
```
125+
126+
Opens a file. `path` should be an absolute path starting with a slash
127+
(e.g. `/dir/filename.txt`). `mode` is a string specifying access mode. It can be
128+
one of "r", "w", "a", "r+", "w+", "a+". Meaning of these modes is the same as
129+
for `fopen` C function.
130+
131+
Returns *File* object. To check whether the file was opened successfully, use
132+
the boolean operator.
133+
134+
```c++
135+
File f = SPIFFS.open("/f.txt", "w");
136+
if (!f) {
137+
Serial.println("file open failed");
138+
}
139+
```
140+
141+
#### openDir
142+
143+
```c++
144+
SPIFFS.openDir(path)
145+
```
146+
147+
Opens a directory given its absolute path. Returns a *Dir* object. To check if
148+
directory was opened successfully, use the boolean operator, similar to opening
149+
a file.
150+
151+
#### remove
152+
153+
```c++
154+
SPIFFS.remove(path)
155+
```
156+
157+
Deletes the file given its absolute path. Returns *true* if file was deleted successfully.
158+
159+
#### rename
160+
161+
```c++
162+
SPIFFS.rename(pathFrom, pathTo)
163+
```
164+
165+
Renames file from `pathFrom` to `pathTo`. Paths must be absolute. Returns *true*
166+
if file was renamed successfully.
167+
168+
### Directory object (Dir)
169+
170+
The purpose of *Dir* object is to iterate over files inside a directory.
171+
It provides three methods: `next()`, `fileName()`, and `openFile(mode)`.
172+
173+
The following example shows how it should be used:
174+
175+
```c++
176+
Dir dir = SPIFFS.openDir("/data");
177+
while (dir.next()) {
178+
Serial.print(dir.fileName());
179+
File f = dir.openFile("r");
180+
Serial.println(f.size());
181+
}
182+
```
183+
184+
`dir.next()` returns true while there are files in the directory to iterate over.
185+
It must be called before calling `fileName` and `openFile` functions.
186+
187+
`openFile` method takes *mode* argument which has the same meaning as for `SPIFFS.open` function.
188+
189+
### File object
190+
191+
`SPIFFS.open` and `dir.openFile` functions return a *File* object. This object
192+
supports all the functions of *Stream*, so you can use `readBytes`, `findUntil`,
193+
`parseInt`, `println`, and all other *Stream* methods.
194+
195+
There are also some functions which are specific to *File* object.
196+
197+
#### seek
198+
199+
```c++
200+
file.seek(offset, mode)
201+
```
202+
203+
This function behaves like `fseek` C function. Depending on the value of `mode`,
204+
it moves current position in a file as follows:
205+
206+
- if `mode` is `SeekSet`, position is set to `offset` bytes from the beginning.
207+
- if `mode` is `SeekCur`, current position is moved by `offset` bytes.
208+
- if `mode` is `SeekEnd`, position is set to `offset` bytes from the end of the
209+
file.
210+
211+
Returns *true* if position was set successfully.
212+
213+
#### position
214+
215+
```c++
216+
file.position()
217+
```
218+
219+
Returns the current position inside the file, in bytes.
220+
221+
#### size
222+
223+
```c++
224+
file.size()
225+
```
226+
227+
Returns file size, in bytes.
228+
229+
230+
#### name
231+
232+
```c++
233+
String name = file.name();
234+
```
235+
236+
Returns file name, as `const char*`. Convert it to *String* for storage.
237+
238+
#### close
239+
240+
```c++
241+
file.close()
242+
```
243+
244+
Close the file. No other operations should be performed on *File* object after `close` function was called.
106245

107246
## WiFi(ESP8266WiFi library)
108247

0 commit comments

Comments
 (0)