delete first line from txt file after being sent through gprs

hello all,

first i am reading the txt file from sd card line by line …read the first line and sending that string to tcp port
through gprs module sim900 …with no problem…then i want to delete the sent line from sd card so arduino will search next line as a first line in next loop…need guidance in deleting first line from sd card

this is my code:

#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);

File printFile;
String buffer;
boolean SDfound;


void setup() {
  Serial.begin(9600);
  SIM900.begin(9600);
  if (SDfound == 0) {
    if (!SD.begin(10)) {
      Serial.print("The SD card cannot be found");
      while(1);
    }
  }
 

 
}

void loop() {
  SDfound = 1;
  printFile = SD.open("TEST.txt");

  if (!printFile) {
    Serial.print("The text file cannot be opened");
    while(1);
  }
   while (printFile.available()) {
    buffer = printFile.readStringUntil('\n');
    Serial.println(buffer); //Printing for debugging purpose 
    
  Serial.println("TCP Receive :");
  Serial.print("AT\\r\\n");
  SIM900.println("AT"); /* Check Communication */
  delay(5000);
  ShowSerialData(); /* Print response on the serial monitor */
  delay(5000);
  Serial.print("AT+CIPMODE=0\\r\\n"); 
  SIM900.println("AT+CIPMODE=0"); /* Non-Transparent (normal) mode for TCP/IP application */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPMUX=0\\r\\n");
  SIM900.println("AT+CIPMUX=0");  /* Single TCP/IP connection mode */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT=1\\r\\n");
  SIM900.println("AT+CGATT=1"); /* Attach to GPRS Service */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CREG?\\r\\n");
  SIM900.println("AT+CREG?"); /* Network registration status */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGATT?\\r\\n");
  SIM900.println("AT+CGATT?");  /* Attached to or detached from GPRS service */ 
  delay(5000); 
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGDCONT=1,\"IP\",\"ISAFE\"\r\\n");
  SIM900.println("AT+CGDCONT=1,\"IP\",\"ISAFE\""); /* Start task and set APN */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CGACT=1,1\\r\\n");
  SIM900.println("AT+CGACT=1,1"); /* Bring up wireless connection with GPRS */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIFSR\\r\\n");
  SIM900.println("AT+CIFSR"); /* Get local IP address */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");
  SIM900.println("AT+CIPSTART=\"TCP\",\"122.169.113.165\",1883");  /* Start up TCP connection */
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSEND\\r\\n");
  SIM900.println("AT+CIPSEND"); /* Send data through TCP connection */
  delay(2000);
  ShowSerialData();
  delay(2000);
  sendtemp();
  delay(5000);
  ShowSerialData();
  delay(5000);
  Serial.print("AT+CIPSHUT\\r\\n");
  SIM900.println("AT+CIPSHUT"); /* Deactivate GPRS PDP content */
  delay(5000);
  ShowSerialData();
  delay(5000);
  break;
  }

/*
 
  
  remove the first line from TEST.txt here



*/





  printFile.close();
}


void ShowSerialData()
{
  while(SIM900.available()!=0)  /* If data is available on serial port */
  Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */
}

void sendtemp(){
 delay(500);
 SIM900.println(buffer);
 delay(300);
 ShowSerialData();
 SIM900.write(0x1A);
 delay(300);
ShowSerialData();
}

otherwise can i do something like this: 1.read the first line from txt file and then send it to tcp port 2.if sent successfully ,set one flag like "sent" or "1"(no need to delete that line) 3.and append that flag at the end of line which is being sent successfully in text file 4.and in next iteration that sent line is to be skipped and next line is to be read from sd card and sent it to tcp port..

is this correct approach? and how can i do this?

The usual way to delete a line from a file is to open the file for read. Read a record.

Open a second file for write.

While not at the end of the first file,
read a record
write the record to the second file

Close the second file.
Close the first file.

Delete the first file.
Rename the second file.

One problem with this approach is that the SD library doesn’t have a way to rename a file, so you have to play the above game twice. The second time, you copy every record, not all but the first.

The second problem is that each open file uses 512+ bytes of SRAM, so two open files uses more than half the memory of a Uno or other 328-based Arduinos.

If you can structure your file so all the records are the same length, you can simply have the first byte of each record mean sent or not sent. You can open a file for read or write. When opening for write, you can append or not. By opening the fixed length record file for read and write, and not append, the you can read a byte, examine it to determine if the record needs processing, and, if it does, read and process it and then move the cursor (using seek()) from its current position (there’s a handy function called position()) to the start of the record, and write the “been there, done that” byte. If the record has already been processed, seek() to the start of the next record, and repeat the process until you find a record that needs processing.