Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FILE_WRITE replaces existing content in File on SD-Card in 2.5.1 #6105

Closed
6 tasks done
Flashmueller opened this issue May 16, 2019 · 4 comments · Fixed by #6106
Closed
6 tasks done

FILE_WRITE replaces existing content in File on SD-Card in 2.5.1 #6105

Flashmueller opened this issue May 16, 2019 · 4 comments · Fixed by #6106
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@Flashmueller
Copy link

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [ESP-12]
  • Core Version: [latest git hash or date]
  • Development Env: [Arduino IDE 1.8.9]
  • Operating System: [MacOS]

Settings in IDE

  • Module: [Generic ESP8266 Module and Adafruit Feather Huzzah]
  • Flash Mode: [qio]
  • Flash Size: [4MB/1MB]
  • lwip Variant: [v1.4|v2 Lower Memory|Higher Bandwidth]
  • Reset Method: [ck|nodemcu]
  • Flash Frequency: [80Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [921600] (serial upload only)

Problem Description

After updating to 2.5.1, data that gets pushed to an existing file on the SD-Card via
file.println(data); gets no longer added below the last line in the file, but replaces the whole file content. So only the last data, that was pushed to the file, stays there.

I was not able to figure out whether the whole file is replaced or only the content.
Same issue with short and long file name.

Examples to reproduce the Error: SD/Datalogger or SD/ReadWrite

Or the one below.

Output on Serial Monitor at readout should be:

test.txt:
testing 1, 2, 3.
testing 4, 5, 6.

But only is:

test.txt:
testing 1, 2, 3.

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

File myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }

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

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

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();

    myFile.println("testing 4, 5, 6.");
    myFile.close();
    Serial.println("done.");
  } 
else {
    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
}

Debug Messages

none

@Flashmueller Flashmueller changed the title FILE_WRITE replacing existing content in File on SD-Card in 2.5.1 FILE_WRITE replaces existing content in File on SD-Card in 2.5.1 May 16, 2019
@earlephilhower
Copy link
Collaborator

The listed example has the sequence which is guaranteed to fail:

    myFile.println("testing 1, 2, 3.");
    myFile.close();

    myFile.println("testing 4, 5, 6.");
    myFile.close();
    Serial.println("done.");

After the first close, the writing of the 2nd line will fail since the file is no longer opened.

@earlephilhower earlephilhower added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label May 16, 2019
earlephilhower added a commit to earlephilhower/Arduino that referenced this issue May 16, 2019
@earlephilhower
Copy link
Collaborator

That said, upon looking at the code I do see the difference. The attached PR reverts to the old behavior. You can try it yourself in your code by simply adding | sdfat::O_APPEND to the define in the header.

@Flashmueller
Copy link
Author

Oh sorry... of course. i was to ambitious cutting out unnecessary parts to provide a short example.
correct Code-Snippet to show the behavior with and without the already requested modification:

  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("1st write done");
  }

myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 4, 5, 6.");
    myFile.close();
    Serial.println("2nd write done");
  } 

THANK YOU FOR that quick fix!
I have tested it and can confirm that it solves the "problem" and makes the new wrapper work just like the old library in this case.

@earlephilhower
Copy link
Collaborator

Reopening to track the fix commit

earlephilhower added a commit that referenced this issue May 17, 2019
* Make FILE_WRITE append in SD.h wrapper

Fixes #6105

* Update host tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants