-1

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?

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();
}
  • Do C++ strings still need the '\0' char at the end? – Ashlyn Black Dec 20 '15 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 '15 at 5:59
0

Try using dataFile.write(arrayName);

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

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.