0

I'm trying to write into a file using this code from the example of the library:

String dataString = "";
int sensor = 0;
dataString += String(sensor);
dataString += ",";
sensor +=1;

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
  dataFile.println(dataString);
  dataFile.close();
}

I have a char array that I want to write in the file, but it works only with String objects apparently. What can I do?

2 Answers 2

1

Try using dataFile.write(arrayName);

https://www.arduino.cc/en/Reference/FileWrite

0

You can write char arrays? What makes you think you can't? Example:

char dataBuffer [80];
strcpy (dataBuffer, "Hello, world");

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
  dataFile.println(dataBuffer);
  dataFile.close();
}
2
  • Do C++ strings still need the '\0' char at the end? Dec 20, 2015 at 18:12
  • Depends on the type of string you are talking about. The strings manipulated by the standard library (eg. strcpy, strcmp, strcat) expect strings to have a 0x00 byte at the end.
    – Nick Gammon
    Dec 21, 2015 at 5:59

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.