ESP32C3-IDF SPIFFS文件系统

1.创建分区

 2.挂载SPIFFS文件系统

          ESP_LOGI(TAG, "Initializing SPIFFS");

    esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",
      .partition_label = NULL,
      .max_files = 5,
      .format_if_mount_failed = true
    };

    // Use settings defined above to initialize and mount SPIFFS filesystem.
    // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
    esp_err_t ret = esp_vfs_spiffs_register(&conf);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount or format filesystem");
        } else if (ret == ESP_ERR_NOT_FOUND) {
            ESP_LOGE(TAG, "Failed to find SPIFFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }

    size_t total = 0, used = 0;
    ret = esp_spiffs_info(conf.partition_label, &total, &used);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
        esp_spiffs_format(conf.partition_label);
        return;
    } else {
        ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }

    // Check consistency of reported partiton size info.
    if (used > total) {
        ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
        ret = esp_spiffs_check(conf.partition_label);
        // Could be also used to mend broken files, to clean unreferenced pages, etc.
        // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
            return;
        } else {
            ESP_LOGI(TAG, "SPIFFS_check() successful");
        }
    }

这里就不给注释了,详细请移步到乐鑫官网

3.创建文件,这里使用C语言标准库去创建fopen

    //创建一个文件
    FILE *f;
    f = fopen(file, "w");
    if(f == NULL)
    {
        printf("创建文静失败\n");
    }
    //创建成功后关闭文件
    fclose(f);
    ESP_LOGI(TAG, "创建文件成功");

4.将用户自定义数据写入文件系统55

void file_write(void)
{
 typedef struct 
{
    int a;
    int b;
    int c;
}DATA;

DATA data = {0};//这里使用结构体去写数据

    data.a = 11;
    data.b = 12;
    data.c = 13;
    printf("write data >>>>>>>>>>>\n");
    printf("%d\n", data.a);
    printf("%d\n", data.b);
    printf("%d\n", data.c);
    printf(">>>>>>>>>>>>>>>>>>>>>>\n");\
    //将以上数据写入文件系统
     FILE *f;
     f = fopen(file,"w");
     if(f == NULL)
     {
         printf("write file is error\n");
     }
     fwrite(&data, sizeof(data), 1, f);
     fclose(f);
     ESP_LOGI(TAG, "保存成功");


}

5.读取用户写入文件系统数据,并打印出来

//读取用户的数据
void SPIFFS_readfile(void)
{
     FILE *f;
     f = fopen(file, "r");
     if(f == NULL)
     {
         printf("open file is error\n");
     }
     fread(&data,sizeof(data),1,f);
     printf("%d\n", data.a);
     printf("%d\n", data.b);
     printf("%d\n", data.c);
     fclose(f);
     ESP_LOGI(TAG, "读取成功");
}

 6.成功运行

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DENG YIRU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值