SD.begin fails after the mircoSd is written & arduino resetted

Hello. I have a problem with my microSD card used with an ethernet shiled and a mega 2560.
The problem is that SD.begin(pin) in the setup fails if:

  1. I write the card 2) I don't cancel the files 3) I reset arduino
    If these 3 things do not happen everything works fine (open, write, cancel, create ecc..) for example if i write on a file, cancel it and reset arduino the sdBegin succeed.
    If I read from a computer the file written on the SD it always looks like to be correct.
    This is how I initialize the sd:
   //initialize SD              SETUP->SD
   Serial1.println("Initializing SD card...");
   // disable w5100 SPI while setting up SD
   pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);//for arduino mega http://arduino.cc/forum/index.php/topic,46976.0.html
  
  // 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(53, OUTPUT);
   digitalWrite(53, HIGH);
   
  if (!SD.begin(4)) {
    Serial1.println("cant save on SD!");
    noSd=true;
  }
  else{
    Serial1.println(F("SD inizialized"));
    
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    mioFile = SD.open("valori.txt", FILE_WRITE);  
    if (mioFile) {
      Serial1.print("file opened");
      if(mioFile.available()>0 )
        sdScritta=true;
      mioFile.close();
      Serial1.println("done.");
    } else {
      // if the file didn't open, print an error:
      Serial1.println(F("error opening valori.txt"));
    }

  }

this is where I write:

      mioFile = SD.open("valori.txt", FILE_WRITE);
      if (mioFile) {
        Serial1.println(perServer);    
        mioFile.println(perServer);
	// close the file:
        mioFile.close();
        sdScritta=true;
        Serial1.println("done.");

and here I read one line

        while (mioFile.peek()!='\r' && mioFile.peek()!=EOF)
        {
          stringaTemp+=(char)(mioFile.read());
        }
        Serial1.write(mioFile.read());//cattura il carattere di a capo
        Serial1.write(mioFile.read());
        Serial1.println(stringaTemp);
        Serial1.println("fine di una riga");

and here I cancel the file

    if(mioFile.peek()==-1){
      Serial1.println("cancel old file"); 
      mioFile.close();
      SD.remove("valori.txt");
      mioFile = SD.open("valori.txt", FILE_WRITE);    //create new one
      sdScritta=false;
      nRiga=0;
  }

If I phisycally eject the SD,put it inside again with the same file written and only after I reset it works!
Do you know if anything is wrong?
Thanks in advance for the time you will spent

  1. I don't cancel the files

Do you mean that you don't close the file?

If you aren't closing the file, why not? Not closing the file is a bad idea.

  1. I don't cancel the files
    Do you mean that you don't close the file?

No, I really mean cancel. I always close the file once I have written o read. If I cancel it an re-create before resetting it works. If I write on it and reset (leaving phisically the card inside the slot) the SD.begin in the setup fails

cantore:
No, I really mean cancel.

Please explain exactly what you mean by 'cancel', because it's not a term that has any standard meaning in this context.

Please explain exactly what you mean by 'cancel'

With 'cancel the file' I mean "SD.remove("valori.txt");"

So you don't remove the file?

exactly. The problem only happens if in order:

1)I write on the txt file
2)I Leave the file written (without canceling an recreating it empty)
3)the sd is not phisically removed
4)The arduino reset

After all this things in the new setup() the card is not recognisez.
In all other case (I don't reset, I phisically remove the sd, I cancel the file before resetting, ecc...) everything works without problems

You've got some logic to check the file size after you open it, and I don't understand how that affects the subsequent code. Could you show the sequence of messages printed on Serial if you reset the Arduino without first removing the file?