open file to append

 

hi,

i am writing up a log file from an EA. if the EA is restarted i want it to carry on writing to the same file... but append to the end of the file. initially this did not work for me, so i searched the forum and found https://forum.mql4.com/13774. i applied the directions there and my init() function now looks like:

void init() 
{
   fd = FileOpen(filename, FILE_WRITE|FILE_CSV);
   if (fd < 0) {
      Alert("open ", filename, " failed");
      FormatError();
      return;
   }
   
   if(FileSeek(fd, 0, SEEK_END)) {
      Print("appending to file");
   }
}

when the code starts the file descriptor is shifted to the end (i get "appending to file" in the experts log, so i gather that FileSeek() was successful). but the original file is overwritten. it is quite infuriating that something so simple is presenting such an obstacle! please can somebody tell me what (silly thing) i am doing wrong? thanks!

best regards, andrew.

 

You need file read and file write. file write will just overwrite the existing contents so you need to read in too...

FileOpen(file_name,FILE_CSV|FILE_READ|FILE_WRITE,';');

hth

V

 
thanks, viffer, that did the trick. won't pretend that it makes sense, but i am not fussy since it now works! much indebted!
 
is it true then that FileSeek() only works if the descriptor is open for reading too?
 

It reads the whole file in, seeks the end, appends your line and then writes the whole lot back overwriting the current contents of the file. If you haven't read in anything, there is no end to seek so your line gets written as the first line and then that then overwirtes the current file contents...it doesn't really append to the file, it appends to a temp dataset and then overwites the file.

V

edit: whether that is what technically happens, I'm not sure, but descriptively, that's how I understand the process.

 

If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.

 
hi phy and viffer, thanks for clarifying the situation!
 
the append works ok when using FileSeek() with FILE_READ | FILE_WRITE, but how about if you need to monitor the file from another application i.e. if you open the file with FILE_SHARE_READ | FILE_WRITE ?
 
ah yes, the mql genie... you need to use FILE_READ|FILE_SHARE_READ|FILE_WRITE, since FILE_SHARE_READ does not load the file... thanks genie ;-)
 
Thank you its working with FileSeek(fd,0,END); FileWrite(fd,msg);
 

Hi all, I still got the problem that all my existing data/lines are overwritten.

I am using the creation of a CSV file to export some numbers during testing, every time when a test has ended a new line with the results should be appended. But it does not work.

Can someone help me, what am I doing wrong. Thank you.

bool WriteFile()
{
   bool toReturn = true;
   
   //File 
   string fName = "TestResults.csv";
   
   //Open or creates file
   int fInt = FileOpen(fName, FILE_CSV|FILE_READ|FILE_WRITE|FILE_ANSI,";");
   
   if(fInt!=INVALID_HANDLE)
   {
      //Go to the end of the file
      FileSeek(fInt, 0, SEEK_END);
      
      //Write to file
      if(!FileWrite(fInt,i_Tipo,i_Period,i_Gap,i_Close,i_MinGap,T,t_p,t_n,U,u_p,u_n,N,n_p,n_n))
      {
         toReturn=false;
      }
   }
   
   //Close the file
   FileClose(fInt);
   
   return(toReturn);
}