-->
Page 1 of 2

How to write binary data (non text) to SPIFFS?

PostPosted: Thu Feb 28, 2019 5:29 pm
by Djames Suhanko
Hello, everyone!
The last two days I'm trying write binary data to SPIFFS, but I face a few questions and problems.
1 - is there no "wb" mode to write binary files to SPIFFS?
2 - Why using POSIX style causes exception to?

I tried this snipest code below:
Code: Select allFILE *ptr;
      byte bb[1];
      bb[0] = 0xD8;
      ptr = fopen("/teste.bin", "wb");
      fwrite(bb, 1, 1, ptr);
      fclose(ptr);


And with SPIFFS method, I could write file, but the mode FILE_WRITE doesn't writes them as binary data.
Some tip, please?

Re: How to write binary data (non text) to SPIFFS?

PostPosted: Sat Mar 02, 2019 3:39 am
by Pablo2048
Read this https://arduino-esp8266.readthedocs.io/ ... ystem.html first. Then - because the file object is a stream class IMO you can use ordinary f.write(buffPtr, size) ... (GOOGLE, Google, google next time... You probably get the answer faster than spent two days doing silly things...)

Re: How to write binary data (non text) to SPIFFS?

PostPosted: Mon Mar 04, 2019 3:45 am
by quackmore
from SPIFFS wiki:

Code: Select allSpiffs' API is posix-like, but not fully posix compliant.


from spiffs.h

Code: Select all/**
/* Any write to the filehandle is appended to end of the file */
#define SPIFFS_APPEND                   (1<<0)
#define SPIFFS_O_APPEND                 SPIFFS_APPEND
/* If the opened file exists, it will be truncated to zero length before opened */
#define SPIFFS_TRUNC                    (1<<1)
#define SPIFFS_O_TRUNC                  SPIFFS_TRUNC
/* If the opened file does not exist, it will be created before opened */
#define SPIFFS_CREAT                    (1<<2)
#define SPIFFS_O_CREAT                  SPIFFS_CREAT
/* The opened file may only be read */
#define SPIFFS_RDONLY                   (1<<3)
#define SPIFFS_O_RDONLY                 SPIFFS_RDONLY
/* The opened file may only be written */
#define SPIFFS_WRONLY                   (1<<4)
#define SPIFFS_O_WRONLY                 SPIFFS_WRONLY
/* The opened file may be both read and written */
#define SPIFFS_RDWR                     (SPIFFS_RDONLY | SPIFFS_WRONLY)
#define SPIFFS_O_RDWR                   SPIFFS_RDWR
/* Any writes to the filehandle will never be cached but flushed directly */
#define SPIFFS_DIRECT                   (1<<5)
#define SPIFFS_O_DIRECT                 SPIFFS_DIRECT
/* If SPIFFS_O_CREAT and SPIFFS_O_EXCL are set, SPIFFS_open() shall fail if the file exists */
#define SPIFFS_EXCL                     (1<<6)
#define SPIFFS_O_EXCL                   SPIFFS_EXCL
...
 * Opens/creates a file.
 * @param fs            the file system struct
 * @param path          the path of the new file
 * @param flags         the flags for the open command, can be combinations of
 *                      SPIFFS_O_APPEND, SPIFFS_O_TRUNC, SPIFFS_O_CREAT, SPIFFS_O_RDONLY,
 *                      SPIFFS_O_WRONLY, SPIFFS_O_RDWR, SPIFFS_O_DIRECT, SPIFFS_O_EXCL
 * @param mode          ignored, for posix compliance
 */
spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode);

/**
 * Writes to given filehandle.
 * @param fs            the file system struct
 * @param fh            the filehandle
 * @param buf           the data to write
 * @param len           how much to write
 * @returns number of bytes written, or -1 if error
 */
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len);


so don't think "wb" is supported but you may want to double check with SPIFFS author

anyway I'm probably missing something in your question ...
SPIFFS_write will just copy memory without caring if it it's text or not

Re: How to write binary data (non text) to SPIFFS?

PostPosted: Mon Nov 30, 2020 6:46 am
by Wenal
I had a similar problem, and to find it I found no other way out but to turn to professionals. They explained the problem to me, and I succeeded.