MQL4 Replace line in CSV file

 

So that can I save the important info to me from each trade, I made code to search for the ticket number (first column) while inside a for loop looking at all open orders. The purpose of this code is to try and over write one line in a CSV file with new information for that line/ticket. Once the ticket is closed, the line is no longer updated and contains the last info written. The problem is, it doesn't work. It adds lines instead of replacing lines. Does anyone know what I'm doing wrong or how to fix it? Thanks much in advance!

Kindest regards,

Don

FileSeek(TradeInfoHandle,0,SEEK_SET);
if(TradeInfoHandle > 0) {
   // Read From File
   int TicketNumberLength = StringLen(OrderTicket());
   while(FileTell(TradeInfoHandle) < FileSize(TradeInfoHandle)) {
      if(FileReadString(TradeInfoHandle) == IntegerToString(OrderTicket()) { 
         FileSeek(TradeInfoHandle,-TicketNumberLength,SEEK_CUR); 
      } else FileSeek(TradeInfoHandle,0,SEEK_END);
   }
   FileFlush(TradeInfoHandle);
}
if(TradeInfoHandle > 0) {   
   //Write To File
   string DataToWrite = StringConcatenate(
   IntegerToString(OrderTicket()),";",
   columns info to write, with each column separated by ,";",
   last column's info does not have ,";", after it
   );
   FileWrite(TradeInfoHandle, DataToWrite);
   FileFlush(TradeInfoHandle);
}
 

I changed:

FileSeek(TradeInfoHandle,-TicketNumberLength,SEEK_CUR); 

to:

FileSeek(TradeInfoHandle,-(TicketNumberLength+1),SEEK_CUR); 

Not perfect, but better results than I got before.

if anyone has any ideas how to do this better I am still interested and thank you in advance for sharing your insight.

Kindest regards,

Don

 
disbellj: try and over write one line in a CSV
  1. Can't be done when lines are variable width (IntegerToString is variable)
  2. Can't be done easily when lines are fixed (your seek attempt)
  3. Just read the entire file in to an array of strings, modify the one line, write the array back out. Adding lines - add another element. deleting lines don't write a line out.
Reason: