Yun and SD Card fileIO.Write() overwrites entire file

Hello!

I'm trying to figure out a small issue I have. On the Arduino Yun, when I write to an SD card with the card in FILE_WRITE, It deletes the entire contents of the card, except for what I wanted to write. However, it writes it in the correct spot. Here is my code.

void addCard()
{
  boolean addRoutine = true;
  digitalWrite(readEnable, LOW);
  int value = 0;
  String currentCard;
  boolean cardStatus = false;
  
  flushBuffers();
  while(addRoutine == true)
  {
    blinkGBLED();
    if(rfidSerial.available() > 0) // Waits for something to come on the serial line
    {
      if((value = rfidSerial.read()) == 10) // check for header [Header of data = 10] 
      { 
        cardStatus = false;
        currentCard = getCardID(); // Get the ID, sets readCard = to the read ID
        
        if(currentCard == Master)
        {
          clearLED();
          blinkRedLED();
          addRoutine = false;
          Console.println("Exiting Add Routine");
        }
        else if ((currentCard == Delete)||(currentCard == Add))
        {
          clearLED();
          blinkRedLED();
          addRoutine = false;
          Console.println("invalid card. Add or delete card. Exiting add routine");
        }
        
        else
        {
          if(cardFind(currentCard)>0)
          cardStatus = true;
     
          if(cardStatus == false)
          {
            if(cardFind(Empty)>0)
            {
              int startingSpot = cardFind(Empty);
              Console.println(startingSpot);
              File cardFile = FileSystem.open("/mnt/sd/Cards.txt", FILE_WRITE);
              if(startingSpot == 1)
              cardFile.seek(0);
              else
              cardFile.seek((startingSpot-1)*12);
              
              Console.println(cardFile.position());
              
              unsigned int bufferLength = 11;
              char test23[11];
              currentCard.toCharArray(test23,bufferLength);              
              [color=yellow]cardFile.write((byte*)test23,bufferLength);[/color]
              cardFile.close();
              Console.println("card added at spot " + startingSpot); 
            }
            if(cardFind(Empty)==0)
            {
              File cardFile = FileSystem.open("/mnt/sd/Cards.txt", FILE_APPEND);
              cardFile.println(currentCard);
              cardFile.close();
              Console.println("card added to the end");
            }
            
                     //Enter Key into Access List Slot
                clearLED();
                digitalWrite(passPin,HIGH);
                delay(1500);
                digitalWrite(readEnable, LOW);
                break;
          }
          
          if(cardStatus == true)
          {
            clearLED();
            digitalWrite(failPin,HIGH);
            delay(1500);
            digitalWrite(readEnable, LOW);
          }
        }
      }
    }
  }
}

What I am doing is storing the ID's of some RFID tags on the SD Card. Cardfind() is a routine to find what line the card is located on the SD card, and returns that line number.

What I want to do, is if there is a card in the list with all 0's, (a card consists of 10 Hex values) I want to replace that with a good card. I can find that empty spot and seek to it, but when I start writing to it, It writes the information for the card I wanted, but deletes everything else from the card.

What I don't want to do is each time I want to add a card to the middle, I don't want to have to go through the entire file of cards and re-write the entire file.

Any assistance would be greatly appreciated!

A preview of the card file before and after the addition of one card in the middle:

0200805889
0200805888
0200805887
0000000000
0200805886
                                    0200805BB9 0200805BB9

It deletes the entire contents of the card, except for what I wanted to write.

Are you sure it is actually doing this ?

It's fairly normal, that when you write to a file, it deletes the existing file with that name, and starts writing a completely new file with the same name. That's the default behavior of most file systems.

It would be a different abnormal problem, if it also deleted any other files on the card, with different file names.

If you are only trying to use a single file, this might seem to be an obscure distinction.

mjw51881:
What I don't want to do is each time I want to add a card to the middle, I don't want to have to go through the entire file of cards and re-write the entire file.

Even though that would be the smart and normal thing to do. Don't be scared because it looks like extra work, the microcontroller will do it all for you in a matter of milliseconds.

Thank you both for the insight. I guess most of all I was trying to help prevent redundant or extra work for the board as I want to add a web interface to this as well, but the re-writing of the entire file looks unavoidable.