SD Card Initialising

This is a simple one but yet I have been unable to find an answer....

I am using a Catalex SD breakout board (shield...board...??) and have pretty much got it working in various test sketches. I have a question regarding good programming protocols.

How often should an SD card be initialised? My initialisation routine is;

while (!SD.begin()) {
    Serial.println("------------------------------");
    Serial.println("SD Card not found / responding");
    Serial.println("Insert a formatted SD card");
    Serial.println("------------------------------");
    SD.end();
    delay(1000);
  }

Not sure why I put the delay in there nor the SD.end()?!

I will be opening a file, writing to it every 2 seconds for anything up to an hour, closing it then opening another and repeating the cycle on and on.

My first draught of the code has me doing;

Initialise -> OpenFile1 -> Write a line -> CloseFile1 -> Initialise -> OpenFile1 -> Write _> CloseFile1 -> Initialise -> OpenFile2 etc etc

I decided this was a poor excuse for coding so will change it to;

Initialise -> OpenFile1 -> Write - > Write -> CloseFile1 -> OpenFile2 -> Write -> Write -> CloseFile2 etc

ie I initialise once only for each Arduino power up cycle.

In the initial code the .close() command caused the writing but I used the .flush() command in version to to force the write.

Just after some guidance as to whether leaving a file open for a long period is OK or whether closing/reopening after each write is better/safer? Also, without an SD.end() command will I be risking the data integrity of the card when I remove it? Maybe I should be closing the file then doing an SD.end() just incase I decide to pull the card after writing to that last file?

If you don’t close or flush, you risk losing data, because data’s only written to the card in 512 byte pages.

Each loop I am writing only 20-26 characters to the SD card. You say the writes are 512 byte pages. I assume if the buffer reaches 512 bytes it will automatically write but the close or flush commands force it to write with a buffer <512 bytes?

Nothing wrong with me flushing such a small amount of data each loop or should I only include a flush statement before I close a file and let the 512 byte pages write when they are full?

And closing the file is only something to do once I have finished with that file/want to open another?

I want to learn good programming techniques where possible.

I assume if the buffer reaches 512 bytes it will automatically write but the close or flush commands force it to write with a buffer <512 bytes?

Yes. When you close the file, the buffer is written to the file first. When the buffer gets full, it is written to the file.

The flush() function forces a write of the buffer to the file, and is used when you don’t want to close the file or risk loosing data in the buffer, if you lose power.

should I only include a flush statement before I close a file

The close() method calls flush(), so there is nothing to be gained by calling it before close().

And closing the file is only something to do once I have finished with that file/want to open another?

Closing the file is necessary to avoid loosing data/corrupting the file. If you can afford the time required to open/close the file on every pass through loop() (where there is something to write to the file), then you should open/write/close the file each time.

Got it. Thanks a heap