Cant open file in SD card

Hey guys,

I am using a pushbutton to record time and store the data into a file in a SD card but the file doesn't open

#include <SD.h>

File myFile;

const int buttonPin = 2;     // the number of the pushbutton pin
//const int ledPin =  13;      // the number of the LED pin
unsigned long time;
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  // pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
   Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  pinMode(buttonPin, INPUT); 

Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}
myFile = SD.open("test.txt", FILE_WRITE); 
}

  
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    //digitalWrite(ledPin, HIGH); 
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another. 
    if (myFile) { 
    Serial.print("Time: ");
    time = millis();
    myFile.println(time);
 
    //myFile.close();
    } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  }
  else {
    // turn LED off:
    //digitalWrite(ledPin, LOW); 
   
  }
}

can you give me a hint where I have been wrong?

into a file in a SD card

Which SD card? Which SD card reader?

Is pin 10 really the chip select pin for the card reader?

How is the SD card formatted?

Have you tried any of the examples in the SD library, to confirm that the hardware and card are suitable?

Hello I am using an Adafruit sd card adapter! It is using the correct format which is FAT16 and tried the example for read/write from the library and it works fine!

Why it is not creating the file?