error: 'myFile' does not name a type

I tried to write a function to pass the read of SD card to other function.

String read_data();

  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();
      return data;
      //Serial.write(data);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening data.txt");
  }

But I got the error: 'myFile' does not name a type

Could anyone please help me to figure it out? Thank you!

Let's see the whole sketch

I assume that you have #included an SD library

UKHeliBob:
Let's see the whole sketch

I assume that you have #included an SD library

UKHeliBob:
Let's see the whole sketch

I assume that you have #included an SD library

The whole sketch is too long. But yes, I have included SD and SPI library

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

Could someone please help. Thank you!

Unless you post the whole sketch, it's impossible to see where this declaration:

 File myFile;

... is, in the grand scheme of things.

The symptoms are that myFile is declared in a function so its scope is local to that function yet you may be trying to use it in the other function you mention.

If that declaration is local, take it out of the function where it is and make it global.