SD and sdFat libraries does not save data on SD card but file are created.

I am using SD card breakout with Kingston 4GB stranded SD card and I used both standard SD library and sdFat library. but I'm getting following results.

sdFat library

#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {

Serial.begin(9600);
if (!sd.begin(4, SPI_HALF_SPEED)) sd.initErrorHalt();
}

void loop() {

    if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) 
     {
     Serial.print("Writing to test.txt...");
     myFile.println("1, 2, 3");
     myFile.close();
     Serial.println("done.");
      //sd.errorHalt("opening test.txt for write failed");
     }
  else
    {
    Serial.println("Error");
    }
  delay(2000);
}

Serial Monitor

Error
Writing to test.txt...done.
Writing to test.txt...done.
Writing to test.txt...done.
Writing to test.txt...done.
Writing to test.txt...done.

Stranded SD library

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

File myFile;

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


  Serial.print("Initializing SD card...");

   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}

void loop()
{

  myFile = SD.open("test.txt", FILE_WRITE);
  
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
delay(2000);
}

Serial monitor

Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt

files are created correctly but nothing is record, Any idea what is happening and how to overcome this?
I used another SD(ScanDisk 8GB) but result is same.

You might want to try a flush() after the println()

That SD card reader does not have a logic level converter, so the 3.3v inputs on the SD get 5v from the Arduino. That has been known to cause problems.

But when I use 2GB micro SD card with Ethernet shield , same code works perfectly. dose it have this logic level converter with it ? how can I overcome this issue?

The ethernet shield SD card has a logic level converter.

I guess the options are use a logic level converter with your current card, or get a SD card with a converter onboard.

But when I use 2GB micro SD card with Ethernet shield , same code works perfectly.

So, think about what size cards are supported. A FAT32 system is required for 4G cards. There's a reason that the SdFat library (an older version of which is the basis for the SD library) is maintained by a guy with the user name fat16lib.

2 GB -SD card , but 4GB SDHC !!!!
IMHO Arduino library not working with SDHC cards

I have a SanDisk 4GB uSDHC card that works fine with the ethernet shield slot and the SD library. The uSD card is formatted FAT32. I had to reformat it after testing a user's ethernet sketch without disabling my SD SPI, and it messed up the format.

according to the introduction of sdFat library ...
http://weblessons.us/lab/Arduino/Library/SD%20Reader%20-%20Fat32/html/

The Arduino SdFat Library is a minimal implementation of FAT16 and FAT32 file systems on SD flash memory cards. Standard SD and high capacity SDHC cards are supported....
...

Under Hardware Configuration it says...

SdFat was developed using an Adafruit Industries Wave Shield.

The hardware interface to the SD card should not use a resistor based level shifter. SdFat sets the SPI bus frequency to 8 MHz which results in signal rise times that are too slow for the edge detectors in many newer SD card controllers when resistor voltage dividers are used.

The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the 74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the 74LCX245.

If you are using a resistor based level shifter and are having problems try setting the SPI bus frequency to 4 MHz. This can be done by using card.int(true) to initialize the SD card.

:cold_sweat: it seems situation is same for standard SD library..

I format the SD card using SD Formatter 4.0 (from www.sdcard.org). now, only sdFat library open the file without having any issue but no data written in to the SD yet.

Might be worth putting in a Serial.println(myFile) statement after the open, and see what it returns.