SPIFFS使用问题

z45217
Posts: 9
Joined: Sat Sep 17, 2022 1:32 am

SPIFFS使用问题

Postby z45217 » Thu Feb 16, 2023 8:49 am

我使用的是IDF4.4版本,先上代码

Code: Select all

void spiffs_save_test(void)
{
    uint8_t test[] = {0x01, 0x02, 0x03, 0x23, 0x02, 0x10};

    FILE *f = fopen("/spiffs/RFID/01.bin", "rb+");

    fwrite(test, sizeof(uint8_t), 6, f);//写入数据
    printf("/spiffs/RFID/01.bin写人%d个字节\n", (uint16_t)ftell(f));

    fclose(f);//关闭文件,
    f = NULL;//文件指针指向空,避免出现游离指针;
}

void spiffs_read_test(void)
{
    uint8_t i=0;
    uint16_t size=0;
    char file_path[] = "/spiffs/RFID/??.bin\0";

    i=1;
    hex_to_asciistring( i, &file_path[13] );//把十六进制转成字符串,0x01转为“01”

    FILE *f = fopen(file_path, "rb");  //打开文件

    fseek(f,0,SEEK_END);  //指针移到末尾 
    size = (uint16_t)ftell(f);//获取指针偏移量
    ESP_LOGI(TAG, "size=%d,file_path=%s", size, file_path);

    fclose(f);//关闭文件,
    f = NULL;//文件指针指向空,避免出现游离指针;

    for(i=0; i<2 ; i++)
    {
        hex_to_asciistring( i, &file_path[13] );//把十六进制转成字符串,0x01转为“03”

        f = fopen(file_path, "rb");  //打开文件

        fseek(f,0,SEEK_END);  //指针移到末尾   
        size = (uint16_t)ftell(f);//获取指针偏移量
        if( i == 1)
            ESP_LOGI(TAG, "size=%d,file_path=%s", size, file_path);

        fclose(f);//关闭文件
        f = NULL;//文件指针指向空,避免出现游离指针;   
    }
}
void hex_to_asciistring(uint8_t data, char* pascii)
{
    int i;
    for (i = 1; i >= 0; i--, data >>= 4)
    {
        if ((data & 0xf) <= 9)
            pascii[i] = (data & 0xf) + '0';
        else
            pascii[i] = (data & 0xf) + 'A' - 0x0a;
    }
}
下面是打印消息,可以看到读数据时,for循环外面的可以读到6个字节,for里面就为0,我还试过把for改成for(i=1; i<2 ; i++)
则读数据正常,不明白这是为什么,我实际应用需要连续读256个.bin文件判断大小,测试发现读两个就出错了
/spiffs/RFID/01.bin写人6个字节
I (977) spiff: size=6,file_path=/spiffs/RFID/01.bin
I (1137) spiff: size=0,file_path=/spiffs/RFID/01.bin

Who is online

Users browsing this forum: No registered users and 34 guests