#endif
3.4.3. How to Use the SPIFFS File System
1. Initialize the SPIFFS file system by calling esp_spiffs_init.
voidspiffs_fs1_init(void)
{
structesp_spiffs_configconfig;
config.phys_size=FS1_FLASH_SIZE;
config.phys_addr=FS1_FLASH_ADDR;
config.phys_erase_block=SECTOR_SIZE;
config.log_block_size=LOG_BLOCK;
config.log_page_size=LOG_PAGE;
config.fd_buf_size=FD_BUF_SIZE*2;
config.cache_buf_size=CACHE_BUF_SIZE;
esp_spiffs_init(&config);
}
2. Open and create a new file, and write the data in it.
char*buf="helloworld";
charout[20]={0};
intpfd=open("myfile",O_TRUNC|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
if(pfd<=3){
printf("openfileerror\n");
}
intwrite_byte=write(pfd,buf,strlen(buf));
if(write_byte<=0)
{
printf("writefileerror\n");
}
close(pfd);
3. Read date via the file system.
open("myfile",O_RDWR);
if(read(pfd,out,20)<0)
printf("readerrno\n");
close(pfd);
printf("-->%s<--\n",out);