ESP32使用SPIFFS文件系统笔记

基于ESP-IDF4.1

复制代码
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <sys/unistd.h>
 4 #include <sys/stat.h>
 5 #include "esp_err.h"
 6 #include "esp_log.h"
 7 #include "esp_spiffs.h"
 8 
 9 static const char *TAG = "example";
10 
11 void app_main(void)
12 {
13     ESP_LOGI(TAG, "Initializing SPIFFS");
14 
15     esp_vfs_spiffs_conf_t conf = {
16       .base_path = "/spiffs",
17       .partition_label = NULL,
18       .max_files = 5,
19       .format_if_mount_failed = true
20     };
21 
22     //使用上面定义的设置来初始化和挂在spiffs文件系统
23     esp_err_t ret = esp_vfs_spiffs_register(&conf);
24 
25     if (ret != ESP_OK) {
26         if (ret == ESP_FAIL) {
27             ESP_LOGE(TAG, "Failed to mount or format filesystem");
28         } else if (ret == ESP_ERR_NOT_FOUND) {
29             ESP_LOGE(TAG, "Failed to find SPIFFS partition");
30         } else {
31             ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
32         }
33         return;
34     }
35 
36     size_t total = 0, used = 0;
37     ret = esp_spiffs_info(conf.partition_label, &total, &used);
38     if (ret != ESP_OK) {
39         ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
40     } else {
41         ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
42     }
43 
44     //使用POSIX和C标准库函数操作文件
45     //创建一个文件
46     ESP_LOGI(TAG, "Opening file");
47     FILE* f = fopen("/spiffs/hello.txt", "w"); // 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
48     if (f == NULL) {
49         ESP_LOGE(TAG, "Failed to open file for writing");
50         return;
51     }
52     fprintf(f, "Hello World!\n");
53     fclose(f);
54     ESP_LOGI(TAG, "File written");
55 
56     //重命名前检查目标文件是否存在
57     struct stat st;
58     if (stat("/spiffs/foo.txt", &st) == 0) {
59         // 如果存在则删除
60         unlink("/spiffs/foo.txt");
61     }
62 
63     // 重命名原始文件
64     ESP_LOGI(TAG, "Renaming file");
65     if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
66         ESP_LOGE(TAG, "Rename failed");
67         return;
68     }
69 
70     // 打开重命名的文件
71     ESP_LOGI(TAG, "Reading file");
72     f = fopen("/spiffs/foo.txt", "r"); // 以只读方式打开文件
73     if (f == NULL) {
74         ESP_LOGE(TAG, "Failed to open file for reading");
75         return;
76     }
77     char line[64];
78     //从指定的流 f 读取一行,并把它存储在 line 所指向的字符串内
79     fgets(line, sizeof(line), f); 
80     fclose(f);
81     // 查找换行符
82     char* pos = strchr(line, '\n');
83     if (pos) {
84         *pos = '\0'; //放置一个空字符串
85     }
86     ESP_LOGI(TAG, "Read from file: '%s'", line);
87 
88     // 卸载分区并禁用SPIFFS
89     esp_vfs_spiffs_unregister(conf.partition_label);
90     ESP_LOGI(TAG, "SPIFFS unmounted");
91 }
复制代码

 

原文:https://gitee.com/EspressifSystems/esp-idf

posted @   kerwin cui  阅读(3915)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示