How to exit a nested if statement

The attached text file has characters that make it difficult to read, but from your description it sounds like your program has two states: gathering data and reporting data.

After you write the data you could go back to gathering it and report again with another button press. Or you could do nothing but exiting doesn't make much sense on an Arduino.

Check out the following framework, it might work for your application.

// possible program states
const int GATHER_DATA = 0;
const int REPORT_DATA = 1;

int programState = GATHER_DATA;  // current program state

int button_state = LOW;

void setup() {

  // setup code stays the same

}

void loop() {

  if ( button_state == HIGH )
  {
    // time to write data
    programState = REPORT_DATA;
  }

  // act on program state
  switch ( programState )
  {
    case GATHER_DATA:

      // code to read sensors

      break;

    case REPORT_DATA:

      // open file, write to file, close file

      // reset state to gathering data - could add an IDLE state if desired
      programState = GATHER_DATA;
      
      break;

    default:
      break;

  }

}