How to keep previous data when open write and close?

 

Hi, 

I want to write an script which I should use file commands to store some data like names of stocks in it, but it will be overwritten everytime I open, write, and close the file.

What should I do to keep previous file data and write the new data after them on a text file everytime I run the script ?

 
saeeds255: I want to write an script which I should use file commands to store some data like names of stocks in it, but it will be overwritten everytime I open, write, and close the file.

What should I do to keep previous file data and write the new data after them on a text file everytime I run the script ?

If you are asking how to just keep one file and append new data at the end of that file, then use the FileSeek() function (using "SEEK_END") to position the cursor/pointer at the end of the file before writing, thus appending to the end of the file.

Alternatively, use a different filename every time - for example, based on the date and time (including seconds, if used very often) - maybe something in the format "prefix.yyyyddmm.hhmmss.txt" (e.g. "marketdata.20161106.083405.txt").

 
Fernando Carreiro:

If you are asking how to just keep one file and append new data at the end of that file, then use the FileSeek() function (using "SEEK_END") to position the cursor/pointer at the end of the file before writing, thus appending to the end of the file.

Alternatively, use a different filename every time - for example, based on the date and time (including seconds, if used very often) - maybe something in the format "prefix.yyyyddmm.hhmmss.txt" (e.g. "marketdata.20161106.083405.txt").

The first method didn't work but the second solution is not bad, I'm using that now.

Thanks

 
saeeds255: The first method didn't work but the second solution is not bad, I'm using that now.

The first method does work. You must be using it incorrectly. Post your code so that we can offer advice.

EDIT: Also, please read this recent Article about Files with MQL5 (but also applicable to MQL4), especially the section about "Writing to the end of the a text files", which I have quoted here:

Writing to the end of a text file

Sometimes, it is necessary to add one or several new text lines to the existing file leaving the rest of the contents intact. To perform such actions, the file should be opened for reading and writing simultaneously. This means the both flags (FILE_READ and FILE_WRITE) should be specified when calling the FileOpen() function.

int h=FileOpen("test.txt",FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT);

If the file with the specified name does not exist, it is created. If it already exists, it is opened while its contents is kept intact. However, if we start writing to the file at once, its previous contents is deleted since the writing is performed from the beginning of the file.

When working with files, there is such a thing as a "pointer" — numerical value indicating the position, from which the next entry or reading from the file is performed. When opening the file, the pointer is automatically set at the beginning of the file. During the reading or data writing, it is automatically relocated by the size of read or written data. You can relocate the pointer yourself if necessary. To do this, use the FileSeek() function.  

In order to save the previous contents and add the new one to the end of the file, relocate the pointer to the end of the file before writing:

FileSeek(h,0,SEEK_END);

The three parameters are sent to the FileSeek() function: handle, pointer relocation value and position the shift is calculated from. In this example, the SEEK_END constant means the end of the file. Thus, the pointer is shifted by 0 bytes from the end of the file (meaning shifting to its very end).

The final script code for adding to the file is as follows:

void OnStart(){
   int h=FileOpen("test.txt",FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT);
   if(h==INVALID_HANDLE){
      Alert("Error opening file");
      return;
   }
   FileSeek(h,0,SEEK_END);
   FileWrite(h,"Additional line");
   FileClose(h);
   Alert("Added to file");
}

This script is also attached below (sTestFileAddToFile). Launch the script and check the contents of the test.txt file. Each call of the sTestFileAddToFile script adds one line to test.txt.

 
Fernando Carreiro:

The first method does work. You must be using it incorrectly. Post your code so that we can offer advice.

EDIT: Also, please read this recent Article about Files with MQL5 (but also applicable to MQL4), especially the section about "Writing to the end of the a text files", which I have quoted here:

This is my code :

   int file_handle=FileOpen(filename,FILE_WRITE|FILE_TXT);
   if(file_handle!=INVALID_HANDLE)
     {
      FileSeek(file_handle,0,SEEK_END);
      FileWrite(file_handle,str.day," : ",str.mon," : ",str.year,"  ",ThisSymbol);
      FileClose(file_handle);
     }
   else PrintFormat("Failed to open HPlist.txt file, Error code = %d",GetLastError());

It overwrites the txt file content everytime I run it.

 
saeeds255: This is my code: ... It overwrites the txt file content everytime I run it.

Please see the correct way to do it, in my previous post, in which I quoted part of an article which you should have read with attention - namely, the importance of "FILE_READ" as well as "FILE_WRITE" in order to be able to SEEK over the existing part of the file, and I quote (again):

Writing to the end of a text file

Sometimes, it is necessary to add one or several new text lines to the existing file leaving the rest of the contents intact. To perform such actions, the file should be opened for reading and writing simultaneously. This means the both flags (FILE_READ and FILE_WRITE) should be specified when calling the FileOpen() function.

Reason: