Go Down

Topic: Read and Delete the 1st line from a text file (Read 960 times) previous topic - next topic

fredi11

Good afternoon everyone, I'm having a problem by trying to erased the fist string line after reading it.
I have a a text file name "1 hour.txt" which it stores some codes like this ones:

1H234
1H343
1H766

What I'm trying to do is read the first line and print it to the serial monitor after reading I want to remove the line and make a file size smaller. I don't if is clear enough.

So far I can only read the first line but I'm stuck with removing bytes from the text.
Thanks!!!

Paul_KD7HB

Good afternoon everyone, I'm having a problem by trying to erased the fist string line after reading it.
I have a a text file name "1 hour.txt" which it stores some codes like this ones:

1H234
1H343
1H766

What I'm trying to do is read the first line and print it to the serial monitor after reading I want to remove the line and make a file size smaller. I don't if is clear enough.

So far I can only read the first line but I'm stuck with removing bytes from the text.
Thanks!!!
Welcome to the Arduino forum.

I have not use an SD card with an Arduino. Can we assume that is what you are doing? You did not say.

You can't actually do what you want without copying the entire remainder of the file data to a new file.

But you can decide on a byte value that you can always ignore in your program and let that value work the same as if you had deleted the line.

Your program would "seek" the beginning of the line. If it's the first line in the file, then the seek(0) will position you at the beginning of the file. Then "write" the byte you have chosen to be the "deleted" value for the number of bytes in your first line. Remembering your "line" may also include a "cr" and "lf" at the end.

Will this work for you?

Paul

fredi11

Okay let me try what your explaining me.

I don't know if posting my code will work out better so you can understand it.
With this code I can read the first line and print it to the serial monitor.

Thanks for your help.


File file;

void setup() {
 
  Serial.begin(9600);
  initializeSD();
  openFile("1hour.txt");
  Serial.println(readLine());
  closeFile();
}

void loop() {
  // put your main code here, to run repeatedly:

}


void initializeSD()
{
  Serial.println("Iniciando SD...");
  if(!SD.begin(4)){
     Serial.println("No se puedo iniciar");
     return;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
    Serial.println("File closed");
  }
}

//metodo que abre el archivo
int openFile(char filename[])
{
  file = SD.open(filename);
  if (file)
  {
    //Serial.println("File opened with success!");
    return 1;
  } else
  {
    Serial.println("Error opening file...");
    return 0;
  }
}


String readLine()
{
  String received = "";
  char caracter;
  while (file.available())
  {
    caracter = file.read();
    if (caracter == '\n')
    {
      return String(received);
    }
    else
    {
      received += caracter;
    }
  }
  return "";
}

Go Up