FileWrite / FileWriteArray Only Appens.

 
So I'm trying to read from a text file and after reading the information delete that line from the text file.
The reading part I get but when I try to Write with one less in the array it appends the array insted of deleting the contend and then writing with this new array size.

Do I need to FileOpen -> Do the reading part of the code -> FileClose THEN FileDelete THEN OpenFile -> FileWrite with the new array?

this is what I have.

void checkOpenNewTrade()
  {
   string InpFileName="newtrade.txt";
//--- open the file
   ResetLastError();
   int file_handle=FileOpen(InpFileName,FILE_READ|FILE_WRITE|FILE_TXT);
   if(file_handle!=INVALID_HANDLE)
     {
      //int    str_size;
      string str;
      string lines [];
      int lineTotal = (int)FileReadArray(file_handle,lines);
      //string emailData = FileReadString(file_handle);

      Print("-----> ArraySize = "+ArraySize(lines));
      str = lines[ArraySize(lines)-1];
      Print(str);
      // REMOVE LAST LINE FROM Array
      ArrayResize(lines,ArraySize(lines)-1);
      Print("--------------------> ArraySize = "+ArraySize(lines));
      FileWrite(file_handle,"What is happening");
      //FileWriteArray(file_handle,lines,0,ArraySize(lines));
      FileClose(file_handle);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }
 
Mathias HalenSo I'm trying to read from a text file and after reading the information delete that line from the text file. The reading part I get but when I try to Write with one less in the array it appends the array insted of deleting the contend and then writing with this new array size. Do I need to FileOpen -> Do the reading part of the code -> FileClose THEN FileDelete THEN OpenFile -> FileWrite with the new array? this is what I have.
You have to reposition the file pointer with FileSeek() in order to overwrite the data, otherwise you will just be writing at the point where you stopped reading (aka appending).
Documentation on MQL5: File Functions / FileSeek
Documentation on MQL5: File Functions / FileSeek
  • www.mql5.com
FileSeek - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Also, if the new array data is less then the previously written, then the file size is not reduced, and when reading it back it will read all the older extra data as well.

So, in those cases you will have to close the file and reopen it for WRITE only, and the seeking will no longer be necessary.

EDIT: As per documentation ...

There are some specific features of work when you specify read and write flags:

  • If FILE_READ is specified, an attempt is made to open an existing file. If a file does not exist, file opening fails, a new file is not created.
  • FILE_READ|FILE_WRITE – a new file is created if the file with the specified name does not exist.
  • FILE_WRITE –  the file is created again with a zero size.
 
Mathias Halen: So I'm trying to read from a text file and after reading the information delete that line from the text file.

Can't be done. You are reading a text file, the positioning will be variable.

Read the file into an array, delete out of the array, reopen the file for writing and write the array.