SD card write very random, mostly not working

In addition to PaulS, you must disable the w5100 SPI or the SD card read/write will be really erratic.
Also start the serial port before using it.

void setup()
{
  // disable w5100 SPI
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.begin(9600);

  pinMode(SS, OUTPUT);

  if (!SD.begin(chipSelect)) Serial.println("Card failed, or not present");

edit: On an Uno, the w5100 SPI disable is accomplished "by accident". The SD.begin() call sets D10 as OUTPUT and HIGH, disabling the w5100.

However, on a Mega, the SD.begin() call sets D53 to OUTPUT and HIGH, leaving D10 floating. This is bad news for the SD card.