How do I a buffer (char array) containing new line character to SD card using SdFat?

I tried:

File myFile = SD.open("test.txt", FILE_WRITE)
myFile.write(buffer, BUFLEN)

but I get this instead:

0000 0000 0000 0000 0000 0000 0000 3337
3837 3838 3937 3035 2c20 2020 2031 2c20
2020 3531 0a33 3738 3738 3839 3730 352c
2020 2020 322c 2020 2035 310a 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000

If I use the normal method, then it seems that SdFat stop printing at the newline character:


Note: the buffer containing data samples in fixed width csv format

3787889751,    1,   49
3787889752,   10,   49
3787889753,    1,   49
  • 2
    I think we'll need a minimal, complete, verifiable example of your code, because I don't think the problem is what you think it is: your stated output doesn't match your stated string, even before the newline.
    – Mark Smith
    Jun 5, 2017 at 8:24

1 Answer 1


File.print() stops printing at a NULL character (ASCII 0) not a new line. You can stick as many new lines as you like in a string and it will print. Try this example

void loop ()
  Serial.print ("Hello\nMultiline\nPrint\n");
  Serial.print ("Hello\0Multiline\0Print\n");

File.write() writes binary data to a file, it will write BUFLEN bytes, even if you don't give it that many bytes.

Looking at the output from you hex editor your string ends with ... 2020 2035 310a which would be 3 spaces (20), 51 (35 and 31) and a new line (0A). There is also a null (00) after that, but I can't tell how many because you have 0s to pad the output (or fill the file).

If you look further back in the output (3rd col, 3rd row) you can see another newline (0a33) so that proves its not new lines that is stopping your output.

Without your code we can't say whats wrong, but I agree with Mark that its not what you think it is.

To write data to a CSV file I would write each row of data into a struct and then hold those structs within a list. Then have a function that iterated through the list and printed each record to the file. That way you wouldn't need a buffer. If it didn't have to be a CSV then you could just call

myFile.write (currentRecord. sizeof(StructRecord));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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