Read from serial & output in pinmode

Hi All
I’m beginner in arduino and i try to read number from file in SD card and based on this number pinmode will output ,

I have file “TST1” and it contain only number “1” , I need serial to read the file and if this number is “1” then pin 31 in arduino mega will output .

i tried with below but i got error
“exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.”

Code i tried :

/*
  SD card read/write


 */

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



File myFile;

int x = 0;



void setup()


{
pinMode (31,OUTPUT);

  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

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


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("TST1.TXT", FILE_READ);

  // if the file opened okay, write to it:
 if (myFile) {
  while (myFile.available()) {
    Serial.write(myFile.read());
  }

 if (Serial.available() > 0) {
    x = Serial.read();  //gets one byte from serial buffer
    if (x == 1) {
  digitalWrite(31,HIGH);
 }
  myFile.close();

 }
 }
}

Any help !

Please post the full text of the error message, not just the last line. The information you need is further up. You can use the button to copy the full message to clipboard for easy pasting.

Your code is conceptually flawed, though: Look at this part:

myFile = SD.open("TST1.TXT", FILE_READ);

  // if the file opened okay, write to it: //Your comment doesn't match the code.
 if (myFile) {
  while (myFile.available()) {
    Serial.write(myFile.read()); //Okay, so you've printed the file to the serial port...
  }

 if (Serial.available() > 0) {
    x = Serial.read();  //But this byte you receive - if any - will have nothing to do with what you sent to the serial port (unless you've got the TX and RX lines tied together
    if (x == 1) { //and x will be equal to '1' (ascii value 0x31 iirc?) not 1, even if you did have it looping back
  digitalWrite(31,HIGH);
 }
  myFile.close();

How about...

myFile = SD.open("TST1.TXT", FILE_READ);

  // if the file opened okay, write to it: //Your comment doesn't match the code.
 if (myFile) {
  while (myFile.available()) { //look at every character in the file
    if (myFile.read()=='1') { //if any of them match '1' 
      digitalWrite(31,HIGH);  //write the pin high and exit the loop
      break;
}
  }
  myFile.close();

Worked for me , thanks for your support :D