issue of Serial.write() sending String to serial monitor

String data;
File myFile;

  myFile = SD.open("data.txt");
  if (myFile) {
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      data = myFile.read();
      Serial.write(data);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening data.txt");
  }

I got error: no matching function for call to 'HardwareSerial::write(String&)'

I also tried to use Serial.println(), but the serial monitor was getting some random ints like"234,5234"

However, if I use Serial.write(myFile.read()); instead of Seral.write(data); then I got correct outputs.

Thanks for your help.

Assuming you are using the SD library, myFile.read() returns 1 char. There is no need to but that into a String. You should make your variable data a better suited type. (e.g. char or int)

blh64:
Assuming you are using the SD library, myFile.read() returns 1 char. There is no need to but that into a String. You should make your variable data a better suited type. (e.g. char or int)

Thanks for your reply. I changed the type of the variable from String to char. But the output looks wired.

The correct output should like this:

GET /?tea=3&sugar=75&ice=75 HTTP/1.1
Ho
GET /?tea=2&sugar=75&ice=50 HTTP/1.1
Ho
GET /?tea=2&sugar=75&ice=50 HTTP/1.1
Ho
GET /?tea=2&sugar=75&ice=75 HTTP/1.1
Ho
GET /?tea=1&sugar=100&ice=100 HTTP/1.1

GET /?tea=1&sugar=100&ice=100 HTTP/1.1

GET /?tea=1&sugar=75&ice=50 HTTP/1.1

But when I change the variable to char it shows this:

E ?e=&ua=5ie5 TP11
o
E ?e=&ua=5ie7 TP11
o
E ?e=&ua=0&c=0 TP11

E ?e=&ua=0&c=0 TP11

E ?e=&ua=5ie5 TP11
o
E ?e=&ua=5ie7 TP11
o
E ?e=&ua=5ie7 TP11
o
E ?e=&ua=0ie7 TP11

yxr861231:
Thanks for your reply. I changed the type of the variable from String to char. But the output looks wired.

You need to post the latest version of your program so we can see exactly what you have done. The devil is in the detail.

...R

Robin2:
You need to post the latest version of your program so we can see exactly what you have done. The devil is in the detail.

...R

Thanks for reminding. Below is the code that I changed:

char data;
File myFile;
myFile = SD.open("data.txt");
  if (myFile) {
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      data = myFile.read();
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening data.txt");
  }

The only thing I changed is that used char instead of String. Thank you so much for helping me out

yxr861231:
The only thing I changed is that used char instead of String. Thank you so much for helping me out

It's not the only thing you changed. This code is also different from the String version and if you had not changed this it would probably be working

    while (myFile.available()) {
      data = myFile.read();
      Serial.write(myFile.read());
    }

In the earlier version you had

      Serial.write(data);

In the new version you read a character from the file into the variable data and then you ignore it and read another character for use with write()

...R