Append to a file at each tick : why fileseek doesn't work ?

 

In my expert start() I have

         handle = FileOpen(filename, FILE_CSV|FILE_WRITE, ";");
         
         if(handle > 0)
         {
            FileSeek(handle, 0, SEEK_END);
            FileWrite(handle, quote);
            FileClose(handle);
         }   

But I got only one line in my file whereas I did ask to append to the file with FileSeek(handle, 0, SEEK_END);

How to fix this ?

Also do I need to open / close handle at every tick ?

 

try this:

handle=FileOpen(filename, FILE_CSV|FILE_READ|FILE_WRITE, ';');
 
 handle = FileOpen(filename, FILE_CSV|FILE_WRITE, ";"); // File is now empty
  1. Don't truncate the file, append to it.
       int         APPEND   = FILE_CSV|FILE_WRITE|FILE_READ;
       string      nameOPT  = WindowExpertName() + ".OPT";
       int      handleOPT   = FileOpen(nameOPT, APPEND, '~');   if(handleOPT < 1){
          AlertMe( "OptComplete: FileOpen(", nameOPT,") Failed: ", GetLastError() );
                                                                         return;  }
       FileSeek(handleOPT, 0, SEEK_END);
       FileWrite( handleOPT, ...
    
  2. You don't need to open a close each tick, but you won't be able to read the file externally while it is open, and on a crash, not all data will be written to disk (unless you also use fileFlush)
 
WHRoeder:
  1. Don't truncate the file, append to it.
  2. You don't need to open a close each tick, but you won't be able to read the file externally while it is open, and on a crash, not all data will be written to disk (unless you also use fileFlush)


thank you very much I modify my code.

I could not test it because expert doesn't seem to execute after loading successfully : it doesn't even print in terminal

https://www.mql5.com/en/forum/146256

Reason: