Delete the first line in the Micro SD

hi good day, can i ask? can anyone provide my a code in how to delete a first line in the micro sd then update the second line to be the first and so on... please... :frowning: :frowning: :frowning:

Do you mean the first line in a file on an SD card?

AWOL:
Do you mean the first line in a file on an SD card?

Assuming that's what OP means, my guess would be to open the file for reading, and open another one as the target to write to. Then read the first line and do nothing. Then read each other line and write it to the second file.

Remember though, that an SD card file doesn't have lines as such, it has characters. You will need to look for CR's and or LF's, or if there aren't any, some other way of demarcating what you as a human know as lines.

I'd be very surprised if there isn't some code here on the forum or in the playground to do something like this; did you search?

Here’s code to delete first line, fixed of bugs I hope.

It reads characters in and ignores them, until it finds a line feed, hex 10. After that, it writes every character it reads into your output file.

It’s all done with functions from setup(), and has simple checking of the sd card and file names to prevent it dropping through if there are problems.

The names of the input and output need to be edited in near the top of the sketch.

Here’s the code:

/*
  BASED ON....SD card file dump

*/

//put the names of your input and output files below
const char *inputFile  = "5lines.txt"; //<<<<<<<<<<<<<<<<<<<<<
const char *outputFile = "4lines.txt"; //<<<<<<<<<<<<<<<<<<<<<


#include <SPI.h>
#include <SD.h>
File dataFile_IN;
File dataFile_OUT;
bool pleaseCarryOn = false;

const int chipSelect = 4;

void setup()
{
  Serial.begin(9600);

  initSD();
  if (pleaseCarryOn) dumpInputFile();
  if (pleaseCarryOn) creatNewFile();
  if (pleaseCarryOn) dumpNewFile();

}//setup

void loop() {} //do nothing

void initSD()
{
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    return;
  }
  else
  {
    Serial.println("card initialized.");
    pleaseCarryOn = true;
  }

} //initSD

void dumpInputFile()
{
  //open the input file
  pleaseCarryOn = false;
  dataFile_IN = SD.open(inputFile);

  if (dataFile_IN)
  {
    Serial.println("Here's the original file");
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      Serial.write(theChar);
    }
    dataFile_IN.close();
    pleaseCarryOn = true;
  }
  else
  {
    Serial.println("Error opening input file... got the right name?");
  }

}//dumpInputfile

void creatNewFile()
{
  pleaseCarryOn = false;
  //open the input file again at the start
  dataFile_IN = SD.open(inputFile);

  //if output file exists, new stuff will be appended, so delete it first
  SD.remove(outputFile);

  //open new file for output
  //only create new file if we have an input file
  if (dataFile_IN) dataFile_OUT = SD.open(outputFile, FILE_WRITE);


  if (dataFile_IN && dataFile_OUT)
  {
    //skip first line
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      if (theChar == 10)
      {
        Serial.println("");
        Serial.println("Found end of first line, starting to write new file");
        break;
      }
    }

    //resuming at start of second line
    //write incoming chars to new file
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      dataFile_OUT.write(theChar);
    }
    Serial.println("...... new file created");
    pleaseCarryOn = true;

  }
  // if the file isn't open, pop up an error:
  else
  {
    Serial.println("Error opening input or output file");
  }
  dataFile_IN.close();
  dataFile_OUT.close();

}//creatNewFile()

void dumpNewFile()
{
  dataFile_OUT = SD.open(outputFile, FILE_READ); //rewind...
  if (dataFile_OUT)
  {
    //now dump new file as proof
    Serial.println("");
    Serial.println("Here's the new file... it should start on line 2");
    while (dataFile_OUT.available())
    {
      char theChar = dataFile_OUT.read();
      Serial.write(theChar);
    }
    dataFile_OUT.close();
  }
  else
  {
    Serial.println("Error opening new file, it probably hasn't been created... was there an input file?");
  }
} //dumpNewFile

Here’s some output:

Initializing SD card...card initialized.
Here's the original file
1111111111
2222222222
3333333333
4444444444
5555555555

Found end of first line, starting to write new file
...... new file created

Here's the new file... it should start on line 2
2222222222
3333333333
4444444444
5555555555

Just a bump: I edited the code in the previous post: better error checking and now the file names only get typed once. If you had already copied it out, you might not ever be coming back here so now you should see this in your unread.

neiklot:
Here’s code to delete first line, fixed of bugs I hope.

It reads characters in and ignores them, until it finds a line feed, hex 10. After that, it writes every character it reads into your output file.

It’s all done with functions from setup(), and has simple checking of the sd card and file names to prevent it dropping through if there are problems.

The names of the input and output need to be edited in near the top of the sketch.

Here’s the code:

/*

BASED ON…SD card file dump

*/

//put the names of your input and output files below
const char *inputFile  = “5lines.txt”; //<<<<<<<<<<<<<<<<<<<<<
const char *outputFile = “4lines.txt”; //<<<<<<<<<<<<<<<<<<<<<

#include <SPI.h>
#include <SD.h>
File dataFile_IN;
File dataFile_OUT;
bool pleaseCarryOn = false;

const int chipSelect = 4;

void setup()
{
  Serial.begin(9600);

initSD();
  if (pleaseCarryOn) dumpInputFile();
  if (pleaseCarryOn) creatNewFile();
  if (pleaseCarryOn) dumpNewFile();

}//setup

void loop() {} //do nothing

void initSD()
{
  Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
  if (!SD.begin(chipSelect))
  {
    Serial.println(“Card failed, or not present”);
    return;
  }
  else
  {
    Serial.println(“card initialized.”);
    pleaseCarryOn = true;
  }

} //initSD

void dumpInputFile()
{
  //open the input file
  pleaseCarryOn = false;
  dataFile_IN = SD.open(inputFile);

if (dataFile_IN)
  {
    Serial.println(“Here’s the original file”);
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      Serial.write(theChar);
    }
    dataFile_IN.close();
    pleaseCarryOn = true;
  }
  else
  {
    Serial.println(“Error opening input file… got the right name?”);
  }

}//dumpInputfile

void creatNewFile()
{
  pleaseCarryOn = false;
  //open the input file again at the start
  dataFile_IN = SD.open(inputFile);

//if output file exists, new stuff will be appended, so delete it first
  SD.remove(outputFile);

//open new file for output
  //only create new file if we have an input file
  if (dataFile_IN) dataFile_OUT = SD.open(outputFile, FILE_WRITE);

if (dataFile_IN && dataFile_OUT)
  {
    //skip first line
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      if (theChar == 10)
      {
        Serial.println("");
        Serial.println(“Found end of first line, starting to write new file”);
        break;
      }
    }

//resuming at start of second line
    //write incoming chars to new file
    while (dataFile_IN.available())
    {
      char theChar = dataFile_IN.read();
      dataFile_OUT.write(theChar);
    }
    Serial.println("… new file created");
    pleaseCarryOn = true;

}
  // if the file isn’t open, pop up an error:
  else
  {
    Serial.println(“Error opening input or output file”);
  }
  dataFile_IN.close();
  dataFile_OUT.close();

}//creatNewFile()

void dumpNewFile()
{
  dataFile_OUT = SD.open(outputFile, FILE_READ); //rewind…
  if (dataFile_OUT)
  {
    //now dump new file as proof
    Serial.println("");
    Serial.println(“Here’s the new file… it should start on line 2”);
    while (dataFile_OUT.available())
    {
      char theChar = dataFile_OUT.read();
      Serial.write(theChar);
    }
    dataFile_OUT.close();
  }
  else
  {
    Serial.println(“Error opening new file, it probably hasn’t been created… was there an input file?”);
  }
} //dumpNewFile




Here's some output:



Initializing SD card…card initialized.
Here’s the original file
1111111111
2222222222
3333333333
4444444444
5555555555

Found end of first line, starting to write new file
… new file created

Here’s the new file… it should start on line 2
2222222222
3333333333
4444444444
5555555555

hi sir can i ask if how about i got a plenty of txt file to open, not only 1 file. can you help me to provide a code for that, like a function for example. sorry for being a noob…

Deleteline(“1.txt”);
Deleteline(“2.txt”);
Deleteline(“3.txt”);
Deleteline(“4.txt”);

There’s no point quoting back the whole previous post, it makes the thread so loooong.

eternalflame10:
can i ask if how about i got a plenty of txt file to open, not only 1 file.

Have a look at the SD library reference page: there’s a method called openNextFile(). Try that maybe?

eternalflame10:
can you help me to provide a code for that, like a function for example. sorry for being a noob…

What code have you tried so far?