Arduino Mega Data Logging HELP NEEDED!!!!!

Hi I'm new to the world of arduinos and coding and I'm having issues with data logging on my mega, I've followed this tutoial https://www.arduino.cc/en/Tutorial/ReadWrite and all works as it should.

I've then found a tutorial on youtube with recording data from a LDR Arduino Tutorial 5.2 - Data Logging - YouTube

I'm having issues in this part of my code where its going into the else statement

myFile = SD.open("SensorVal.csv", FILE_WRITE);

if (myFile)
  {
    Serial.print("val");
    myFile.print(val);
    myFile.print(",");
    Serial.print(","); 
    myFile.println(millis());
    Serial.println(millis());
    myFile.close();
  }

else

  {

    Serial.println("Error");
  }

Heres the full code and any help will be brilliant. Thank you in advance

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

File myFile;
int pinCS = 53;
int LDR = A0;
int val = 0;
void setup() {
  // put your setup code here, to run once:


Serial.begin(9600);
pinMode (pinCS, OUTPUT);
pinMode (LDR, INPUT);
Serial.println("Initializing SD card");
if (SD.begin())
   {
    Serial.println("Initialization Complete");
    
   }
else 
  {
   Serial.println("Initialization Failed");
}
}

void loop() {
  // put your main code here, to run repeatedly:

val = analogRead(LDR);
myFile = SD.open("SensorVal.csv", FILE_WRITE);

if (myFile)
  {
    Serial.print("val");
    myFile.print(val);
    myFile.print(",");
    Serial.print(","); 
    myFile.println(millis());
    Serial.println(millis());
    myFile.close();
  }

else

  {

    Serial.println("Error");
  }
delay (1000);
}

I have broken this kind of thing into two parts.
One Arduino to do sensor, display and input stuff.
One Arduino to do data logging.

See:
http://forum.arduino.cc/index.php?topic=281091.0

I only have one arduino to do this with

I recommend not to bother with an SD card shield.

It is much, much simpler just to connect the Sparkfun Openlog to a serial port and use serial.Print() statements to send the data to the card.

Well, that's two people suggesting you to use a separate data logger.

.