Correct way of using SD write.

Hi,

Like many developers I code on the principle of 'stop fiddling once it starts working' - which is ok most of the time, but sometimes just because something looks like it's working doesn't mean it's working properly!

I've just checked the reference guide for how I should be using SD:file.write(), and it seems I've been using it wrong, but despite that my code works.

I have a data logger that uses an array of 60 characters (char myBuff[60] ) to store data before writing it to an SD card using

write(myBuff);

The reference seems to suggest that I should be supplying the length of the buffer to the write operation

write(myBuff,60);

This is what the reference says....

Syntax

file.write(data)
file.write(buf, len)

Parameters

file: an instance of the File class (returned by SD.open())

data: the byte, char, or string (char *) to write

buf: an array of characters or bytes

len: the number of elements in buf

My way seems to work, but am I using the correct method? What's the difference (if any) between write(myBuff) and write(myBuff,60)? If there's no difference, why are there two different methods?

Guidance appreciated!

It depends on what you need to do in your program.

  char myBuff[60];

  // This will write up to a zero byte.  Maybe less, maybe more than 60 bytes.
  file.write(myBuff);

  //  This will write all 60 bytes of myBuff independent of the content of myBuff.
  file.write(myBuff, 60);

Thanks fat16lib. clear and concise explanation - exactly what I was looking for