2

I'm coding a data logger and I would like to know what happens if there are no bytes remaining to be written using SD library, ie, if the SD card is full of data. The logging would just stop or I would get some nasty error?

1
  • 1
    Unless you list your code no one can say. – Milliways Jan 2 '16 at 5:27
3

Open-source is just great. Lets have a look at the source code for SD. Here is what happens on write of a block.

/**
 * Write data to an open file.
 *
 * \note Data is moved to the cache but may not be written to the
 * storage device until sync() is called.
 *
 * \param[in] buf Pointer to the location of the data to be written.
 *
 * \param[in] nbyte Number of bytes to write.
 *
 * \return For success write() returns the number of bytes written, always
 * \a nbyte.  If an error occurs, write() returns -1.  Possible errors
 * include write() is called before a file has been opened, write is called
 * for a read-only file, device is full, a corrupt file system or an I/O error.
 *
 */

The source code shows that blocks are allocated from the free list and if there are no more blocks the function will return an error.

1

There doesn't seem to be any definite answer in the library documentation, but I am fairly certain that writes will simply fail, and not cause any sort of corruption. The last log entry may be incomplete, but the file should still be readable.

1
  • 2
    Any of us can speculate in that manner about what might happen, and come up with an answer like that that's hardly worthwhile. I suggest that if you don't know the answer and don't have evidence one way or another, then don't put what you have to say in an "Answer" but instead in a comment, if at all. ¶ To answer a question like this, either examine the library code itself (rather than its documentation) or run an experiment (eg, declare a big file, seek to near its end, write some bytes, repeat until fail, ie until write() < bytecount) – James Waldby - jwpat7 Jan 2 '16 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.