• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

How to clear contents of SD Card file?

Q&A Forum › Category: ESP32 › How to clear contents of SD Card file?
0 Vote Up Vote Down
Jack Douglas asked 3 months ago

I am following instructions in part using an SD Card as described here https://randomnerdtutorials.com/esp32-microsd-card-arduino/ on an esp32.   Sensor data is written to the card, then it is later opened and the data is sent.  Once it is sent, I want to clear the contents.  However, if I use SD.remove(“/data.txt”) the data is cleared, but the following error occurs :

[ 27102][E][vfs_api.cpp:104] open(): /sd/data.txt does not exist, no permits for creation
End Data
[ 29103][E][vfs_api.cpp:175] remove(): /data.txt does not exists or is directory

How to avoid this from occurring?

I guess I could delete the file altogether and create it again each time the program runs.  Seems like there should be a simple way to clear the contents.

Question Tags: ESP32, SD card
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 months ago

Hi.
Can you try the following snippet? It opens the file, moves the cursor to the beginning of the file, removes all contents, and closes the file. I didn’t test it.
 

File file = SD.open("data.txt", FILE_WRITE);
file.seek(0);
file.truncate();
file.close();

 
Tell me if it works.
Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 3 months ago

The following error occurs :

on_120822TEST.ino:389:30: error: 'class fs::File' has no member named 'truncate'
dataFile.truncate();
^~~~~~~~
*** [.pio\build\esp32dev\src\SoilStation_120822TEST.ino.cpp.o] Error 1
0 Vote Up Vote Down
Jack Douglas answered 2 months ago

As per my initial thoughts, I ended up using SD.remove() [https://arduinogetstarted.com/reference/library/arduino-sd.remove], then creating a new file for each loop.

0 Vote Up Vote Down
Sara Santos Staff answered 2 months ago

Hi.
I’m so sorry for taking so long to get back to you.
For some reason, I didn’t receive a notification from your previous response.
 
Thanks for sharing that solution.
 
Regards,
Sara

0 Vote Up Vote Down
Jack Douglas answered 2 months ago

No worries, thanks Sara.

0 Vote Up Vote Down
Sara Santos Staff answered 2 months ago

I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.