Serial and SD card communication problems

I have coded a program and it worked fine for me as I wished. I have send the data to serial port using Serial.print and also save it to an SD card by using the SD library. I tested the code and find the following problem:

I let the code to output a count for the loop runs, namely when the loop runs for the first time, it will output 1, then another run will be 2, etc. ....... But I got the following bizarre results:

counts
1
2

4
5
6
7
78
9
......

Besides this, all the code runs no problem. Anyone can help me out?
Thanks in advance.

Without seeing the code?

sterretje:
Without seeing the code?

Here is the code:

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

int Infred = 5;
int Counter = 0;

void setup() {
// put your setup code here, to run once:
const int chipSelect = 4;
SD.begin(chipSelect);
File dataFile = SD.open ("DATA.log", FILE_WRITE);
dataFile.close();

Serial.begin (9600);
pinMode(Infred, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:

File dataFile = SD.open ("DATA.log", FILE_WRITE);
Counter = Counter + 1;

while (digitalRead(Infred) == HIGH) {

}

Serial.println (Counter);
dataFile.println (Counter);

dataFile.close();
delay(1000);
}