SD card can't open file

I'm using the standard SD.h library and the Datalogger example runs fine, but when I attempt to make my own datalogger using a ToF sensor (MTOF17001) (which is giving the correct data in its example code) it can't open the file.

SD Example:

/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

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

const int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  


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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

My attempt:

/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created  24 Nov 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>
int incomingByte = 0;
char sendCommand;
char c;
int con;
int interval;
long distance;
const int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  delay(333);//recover memory time

  // read three sensors and append to the string:
  dataString += millis() / 1000;
  dataString += ", ";

  sendCommand = 0x55;
  Serial.write(sendCommand);
  sendCommand = 0xAA;
  Serial.write(sendCommand);
  sendCommand = 0xD3;
  Serial.write(sendCommand);
  sendCommand = 0x00;
  Serial.write(sendCommand);
  sendCommand = 0x02;
  Serial.write(sendCommand);
  sendCommand = 0xD5;
  Serial.write(sendCommand);
  con = 0;
  distance = 0;

  while (true) {
    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      con++;//get next data

      if (con == 6) {
        distance = incomingByte;
      }

      if (con == 7) {
        distance = (distance << 8) + incomingByte;//combine data 6 & 7
        Serial.print("Distance=> ");
        Serial.print(distance);
        dataString += String(distance);
        Serial.println("mm");
      }
    } else {  //no value -> break out the while loop
      break;
    }
  }
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  writeDat(dataString);
}

void writeDat(String datas) {
  Serial.println(datas);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  File dataFile = SD.open("DATALOG.TXT", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(datas);
    dataFile.close();
  }
  else {
    Serial.println("error"); //this is the error that is being returned, the sd card is initializing but cant open the file
  }
}

Working sensor example:

/******************************************
  SHARP Corporation

  MTOF17001 TOF module
  for Arduino Mega2560 V1.0
  Working range : 10cm~120cm (White Card)

  Board Connection
  Arduino   ToF module
  0pin      TXD 
  1pin      RXD
  GND       GND
  3.3V      Vcc
  Serial monitor setting
  9600bps

  Example instructions:
  @ Command name:CMD_RD_MM
  @ Send instruction:55 AA D3 00 02 D5
  @ Return instruction:55 AA D3 00z 02 01 02 D8 -> get value 6 & 7 -> 0x0102=258mm
  @ Distance measure data( reply the "8888" when abnormal status)
 ******************************************/

int incomingByte = 0;
char sendCommand;
char c;
int con;
int interval;
long distance;

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(100);//recover memory time

  sendCommand = 0x55;
  Serial.write(sendCommand);
  sendCommand = 0xAA;
  Serial.write(sendCommand);
  sendCommand = 0xD3;
  Serial.write(sendCommand);
  sendCommand = 0x00;
  Serial.write(sendCommand);
  sendCommand = 0x02;
  Serial.write(sendCommand);
  sendCommand = 0xD5;
  Serial.write(sendCommand);

  con = 0;  
  distance = 0;

  while (true) {
    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      con++;//get next data
      
      if (con == 6) {
        distance = incomingByte;
      }

      if (con == 7) {
        distance = (distance << 8) + incomingByte;//combine data 6 & 7
       Serial.print("Distance=> ");
       Serial.print(distance);
       Serial.println("mm");
      }
    } else {  //no value -> break out the while loop
        break;
    }
  }
}

I don't use String objects, but maybe change

dataString += millis() / 1000;

by

dataString += String(millis() / 1000);

There might be other places where you do similar things with numbers.

Not the cause of your problem, but why do you have while (!Serial) in the middle of nowhere?