string format in SD card library

hi, im newbie in this thing. i want to ask, i use two xbee to transmit data but in receiver i have no idea how to change string format so i can't use "FIle.write" command
the error output was : "no matching function for call to 'File::write(String&)'"

here's my code

void loop() {
  
  if (Serial.available()) {  
  sensor = Serial.read();
  String datasensor = getData();
  File = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    File.write(datasensor);
    File.close();
    Serial.println("done.");
  }
  else {
     Serial.println("error opening test.txt");
  }
}
}
String getData(){
   String data = "";
  data+="sensor =";
  data+=" : ";
 data+=sensor; 
  return data;
}

The error message is pretty self-explanatory - there is no method defined for writing a String.
You can write a single byte (uint8_t)

size_t write(uint8_t);

or a buffer, by specifying its length

size_t write(const uint8_t *buf, size_t size);

Could do this:

File.write( &datasensor[0], datasensor.length() );

AWOL:
The error message is pretty self-explanatory - there is no method defined for writing a String.
You can write a single byte (uint8_t)

size_t write(uint8_t);

or a buffer, by specifying its length

size_t write(const uint8_t *buf, size_t size);

is that added on SD.h lib? i've try it and still the same error warning

pYro_65:
Could do this:

File.write( &datasensor[0], datasensor.length() );

the output: invalid conversion from 'char*' to 'const uint8_t*'.
i just want to write a string from getData(). as i did before, without string in getData() i've success write it in txt

here's my entire code:
sender:

void setup()
{
Serial.begin(9600);
}

void loop()
{
  for(int x = 0; x<10; x++){
int analogValue = analogRead(3);
int val = map(analogValue, 0, 1023, 253, 0); 
Serial.println(val);
delay(1000);
  }
  while(true);
}

receiver:

#include<SD.h>

int sensor;

File myFile;

void setup() {
Serial.begin(9600);
  while (!Serial){
  ;
  }
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop() {
  if (Serial.available()) {  
  sensor = Serial.read();
  String datasensor = getData();
  myFile = SD.open("datalog.txt", FILE_WRITE);
  if (myFile) {
    myFile.write(datasensor); 
    myFile.close();
    Serial.println("done.");
  }
  else {
    Serial.println("error opening datalog.txt");
  }
}}

String getData(){
  String data = "";
  data+="sensor =";
  data+=" : ";
 data+=sensor; 
  return data;
}

A simple cast should fix:

File.write( ( uint8_t* ) &datasensor[0], datasensor.length() );

&datasensor[0] returns a char*, the function just needs its unsigned counterpart. ( unsigned char == uint8_t )

Perhaps what you really need to do is explain why you are wanting to use write() to store binary data in the file, when what you have is a String (that wraps a string) that contains ASCII data. Using the print() methods would make more sense to me.

PaulS:
Perhaps what you really need to do is explain why you are wanting to use write() to store binary data in the file, when what you have is a String (that wraps a string) that contains ASCII data. Using the print() methods would make more sense to me.

because if i'm using print() the data receive in serial monitor receiver is the ASCII data. i have to combine the sensor data with String time

because if i'm using print() the data receive in serial monitor receiver is the ASCII data.

I don't understand this statement. Perhaps you should show the Serial Monitor contents when you use print(), and explain what you expected to see, instead.

Although, really, the relationship between using print() to print to the Serial Monitor and using print() to write to the SD card escapes me.

i have to combine the sensor data with String time

Time shouldn't be in a String, so you shouldn't need to do this, either.