File on SD Card don't close proper.

Hello,

i use the readwrite example and my own sketch open the File with "SD.open", write with "dataFile.println" and close the file with "dataFile.close()". When i open the SD Card with Ubuntu 16.04 i see the File, but it contens only tangled signs. Surprised is, when i create a new file the old file is correct and readable as text and the new is not readable. This is repeated, work the close() command not properly?

regards ThomasD

You need to post your code.

Hello,
Here the code:

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#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 native USB port only
  }


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

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  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");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

regards ThomasD

Which SD card module or shield are you using? If a module, how is it wired?

edit: It takes a bit of power to write. Where are you getting power for the SD card?

Hello,

i thing it's solved. I use now an old 2GB Sandisk and have no Problem.

regards ThomasD

Hi,

Great that it is solved. Another alternative could have been that you dont have to close the file after every iteration in the loop as you keep on writing data in the file.