SD programs only write one line and then overwrites it

I have tried everything I know how but I can only get my SD card programs to write one line of information. Then it overwrites it on the next loop. Simple program attached.

PLEASE HELP

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

const int chipSelect = 10;
int loopCount;

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

 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:
   return;
 }
 Serial.println("card initialized.");
}

void loop() {

 if (loopCount<11)
 {
 for (int loopCount = 0; loopCount < 20; loopCount++) 
 {
 File dataFile = SD.open("datalog4.txt", FILE_WRITE);

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

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

I believe you're encountering this bug:

it can be solved by doing this:

  • Sketch > Include Library > Manage Libraries
  • Wait for download to finish
  • Type: > Arduino
  • In the "Filter your search..." box, type "sd"
  • Scroll down through the results until you see the "SD by Arduino, Sparkfun" entry. Click on it. It should show that you have version 1.2.0 installed.
  • Click "Update". This should install version 1.2.1.
  • Wait for update to finish
  • Click "Close"
  • Upload the sketch to your board again. The problem should no be solved.

THANKS. You were dead on. I imagine many have this issue.

I added the code tags you suggested.

pert:
I believe you're encountering this bug:
FILE_WRITE does not work properly since v1.2.0 · Issue #45 · arduino-libraries/SD · GitHub
it can be solved by doing this:

  • Sketch > Include Library > Manage Libraries
  • Wait for download to finish
  • Type: > Arduino
  • In the "Filter your search..." box, type "sd"
  • Scroll down through the results until you see the "SD by Arduino, Sparkfun" entry. Click on it. It should show that you have version 1.2.0 installed.
  • Click "Update". This should install version 1.2.1.
  • Wait for update to finish
  • Click "Close"
  • Upload the sketch to your board again. The problem should no be solved.

You cannot imagine how many times I went through my code rewriting it, recombining it. I tried everything until tonight I found this post and now it works fine.
Thank you, thank you.