Delete only contents of text file.

Hi all,

I need to delete only contents of a text file in sd card. Whether any function available..??

Scenario: Need to read value from a text file in sd card. And converting that into number.(i am having code to do that) Increment value by 1. Replace the current value (incremented by 1) with previous value in the same text file.

Is there any function available to delete only content in arduino..??

Thanks in advance.

#include <SD.h>
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins from the above pin mappings
LiquidCrystal lcd(3,4,5,6,7,8); // 

File myFile;
File myFile2;
File myFile3;
static int x,y,z;
String inString = "";    // string to hold input

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

  // send an intro:
  Serial.println("\n\nString toInt():");
  Serial.println();
   pinMode(10, OUTPUT);
  
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    lcd.print("init fail");
    delay(5000);
    lcd.clear();
    return;
  }
  Serial.println("initialization done.");
  lcd.print("init done");
    delay(5000);
    lcd.clear();
  
}

void loop() {
  lcd.print("void loop");
  delay(5000);
  lcd.clear();
  
   myFile = SD.open("radiant.txt");
   if (myFile) {
  // Read serial input:
   while (myFile.available()){
    int inChar = myFile.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char 
      // and add it to the string:
      inString += (char)inChar; 
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Value:");
      x = inString.toInt();
      y = x+10;
      SD.remove("radiant.txt");
      myFile2 = SD.open("radiant.txt",FILE_WRITE);
      myFile2.close();
      myFile2 = SD.open("radiant.txt",FILE_WRITE);
      myFile2.println(y);
      myFile2.close();
       lcd.print(y);
  delay(5000);
  lcd.clear();
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = ""; 
    }
  }
}
myFile.close();
}

Here i tried with SD.remove(); but it does not works. Please help me with this issue.

Sd.remove()-delete the entire text file.

i need to delete ony the content.

i need to delete ony the content.

And leave the file? Open the file for write (not append). Close the file. Poof, the contents are gone.

Sorry i dint get you.. whether just opening and closing the file, will delete it contents...??

whether just opening and closing the file, will delete it contents...??

Depending on how you open the file, yes or no. Open for append and close, and the contents remain. Open for write and close, the contents are replaced (with nothing, since you wrote nothing).

I tried with following code. it doesnot works. Can you please help me. I am doing all(reading,replacing incremented value) in single file only.

#include <SD.h>
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins from the above pin mappings
LiquidCrystal lcd(3,4,5,6,7,8); // 

File myFile;
File myFile2;
File myFile3;
static int x,y;
String inString = "";    // string to hold input

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

  // send an intro:
  Serial.println("\n\nString toInt():");
  Serial.println();
   pinMode(10, OUTPUT);
  
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    lcd.print("init fail");
    delay(5000);
    lcd.clear();
    return;
  }
  Serial.println("initialization done.");
  lcd.print("init done");
    delay(5000);
    lcd.clear();
  
}

void loop() {
  lcd.print("void loop");
  delay(5000);
  lcd.clear();
  
   myFile = SD.open("radiant.txt");
   if (myFile) {
  // Read serial input:
   while (myFile.available()){
    int inChar = myFile.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char 
      // and add it to the string:
      inString += (char)inChar; 
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Value:");
      x = inString.toInt();
      y = x+10;
      myFile2 = SD.open("radiant.txt",FILE_WRITE);
      myFile2.close();
      myFile2 = SD.open("radiant.txt",FILE_WRITE);
      myFile2.println(y);
      myFile2.close();
       lcd.print(y);
  delay(5000);
  lcd.clear();
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = ""; 
    }
  }
}
myFile.close();
}

I tried with following code. it doesnot works.

The code does something. In my book, that means that it is working. If you have some other definition of work, you need to explain what the program does, and how that differs from what you want it to do.

Actually what i need to do is, when a input is high i need to read the value which is stored in sdcard(as text file). Then i need to increment that value by 10. Then i need to replace the previous value by current incremented value. reading and replacing values occurs in a single file(in sdcard) only. Hence i am trying to delete the contents after i reading the value. For this scenario i tried the above posted code. Thanks in advance.

The code above i posted does not throw any errors. But it does not works correctly.. :'(

Once again, the code does something. You have not explained what it actually does. You have not explained how that differs from what you want.

Is there no "truncate" function for files?

Is there no "truncate" function for files?

There doesn't need to be. If you open a file for writing, it erases any previous contents. If you then immediately close it, it becomes an empty file.

I think the OP wants to "update" the value in a file. Something like this should work:

fp = open("datafile", "r");
value = int( read(fp, 4) ):
fp.close();

value++;

fp = open("datafile", "w");
write(fp, value);
fp.close();

This is quaranteed to be wrong, but close.

File hFile;
hFile = SDClass::open(“filename”, O_TRUNC | O_WRITE);
if (hFile) {
hFile.close();
}