rewrite the new amount in file

 

hello friends

I have a bot that runs on several currency pairs

I want to save the profit amount of each currency pair in one line of a file

For example:

EURUSD 19.30

USDCAD 8.03

AUDCHF 13.37

 whenever the profit amount changes, I want to find the previous line, correct its value, or delete it, and rewrite the new amount in it.

who can help me to do it?


 
st.khalili:

Please show your attempts (code). Without it we cannot know what you might be doing wrong or help you in what you are trying to do.

And please use the code button and tell us what language you are using (MQL4 or 5).

 

 
Miguel Angel Vico Alba #:

Please show your attempts (code). Without it we cannot know what you might be doing wrong or help you in what you are trying to do.

And please use the code button and tell us what language you are using (MQL4 or 5). 

void Write_file()
  {
   double Profit = 0;
   double NewProfit =0 ;
   string filename = "bookedProfit.csv";
   FileDelete(filename);
   int filehandle = FileOpen(filename,FILE_COMMON|FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI,",");
   if(filehandle != INVALID_HANDLE)
     {
      FileSeek(filehandle,0,SEEK_SET);
      for(int i=0 ; i<2 ; i++)
        {
         if(NewProfit !=Profit)
           {
            NewProfit = Profit;
           } 
           
         FileWrite(filehandle,Symbol()+(string)Period(),NewProfit);
        }

      FileClose(filehandle);
     }
   else
      Print("Failed to open and write ",filename," due to ",GetLastError());
  }

its in mql4, i want to remove previous profit and rewrite new amount

 

My suggestion is :

Everytime profit amount of a pair changes just write the whole file from scratch. 

 
Your topic has been moved to the section:  MQL4 and MetaTrader 4
 
Yashar Seyyedin #:

My suggestion is :

Everytime profit amount of a pair changes just write the whole file from scratch. 

The file name is common between two robots and both robots need to save information in the same file

Therefore, each robot should only change its own line

 
I know I can make a separate file for each currency and solve my problem easily
But I want to know if there is a way to delete and rewrite a specific line in a file
 
st.khalili #: I know I can make a separate file for each currency and solve my problem easily
But I want to know if there is a way to delete and rewrite a specific line in a file

It is not possible to replace a line without rewritten the entire file from the beginning, because each line is of variable length.

Standard text files (e.g. CSV) have no fixed sized record structure, and cannot be easily modified.

If you want to have a file with shared random access to the data, then you have to use fixed sized record structure, preferably using binary format.