🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Can't add data to existing SD file

    Scheduled Pinned Locked Moved Core2 for AWS
    1 Posts 1 Posters 3.1k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      SteveM
      last edited by

      Hi,
      I have ported my datalogging program from Arduino Mega to the M5Stack Core 2 for AWS.
      It all is now working as I expected except for one behavior.
      On the Arduino, I could open an existing file on SD card and append data to the end of the file.
      On the M5Stack, I can create, open, write to, and close a file on the SD card.
      But when I re-open this file and write more data, it overwrites the existing data in the file.
      It appears the FILE_WRITE specifier is allowing write access but is not moving the file pointer to its end...not sure why not?
      Can anyone suggest what I am doing wrong or provide a workaround?
      Thanks!

      The relevant portion of my test code is below; apologies if it comes through with weird formatting:

      if (!SD.begin(4,SPI,40000000)) { // ******M5 CS line is GPIO4
      Serial.println("initialization failed!");
      while (1);
      }
      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.

      NewFile = SD.open("/TRIAL.TXT",FILE_WRITE);  
      char message1[64]="This is just a test";  // 64 byte array
      char message2[64]="It seems to be working so far!";
      

      // delay(5000);
      // if the file opened okay, write to it:
      if (NewFile) {
      NewFile.println(message1);
      NewFile.println(message2);

      // close the file:
      NewFile.close();
      

      } else {
      // if the file didn't open, print an error:
      Serial.println("error initially opening test.txt");
      }

      // Now try reopening file and writing more data into it

       delay(100);
       NewFile = SD.open("/TRIAL.TXT",FILE_WRITE);  //*******Reopen for more writing
       NewFile.println("Trying to add this additional line");
       NewFile.close();
       delay(100);
      
      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post