Read File - return value of variable

 

Hello,

I'm writing values inside a file,line by line, like :

OrderTicket1=value1

OrderTicket2=value2

OrderTicket3=value3

..etc, with :

      ResetLastError();
      string InpDirectoryName = "Telegram - MT4";
      string InpFileName = "values.ini";
      int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);

      if(file_handle!=INVALID_HANDLE){
         FileSeek(file_handle, 0, SEEK_END);
         FileWrite(file_handle,OrderTicket()+"="+last_message_id);

         //--- close the file
         FileFlush(file_handle);
         FileClose(file_handle);
         PrintFormat("Data is written, %s file is closed",InpFileName);
        }
      else
         PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());

and for example, I would like a way to read "OrderTicket2 " value after the "="


For now I'm doing a simple while loop that only detect if OrderTicket is written in the file but I don't know how to return the value assigned to it.

      double value;   
      while(!FileIsEnding(file_handle))
              {
               value=FileReadNumber(file_handle);
               if(value==OrderTicket())
                  ReadData(file_handle);
              }
         FileClose(file_handle);
        }


//+------------------------------------------------------------------+
//| Read the file's string data                                      |
//+------------------------------------------------------------------+
void ReadData(const int file_handle)
  {
   bool flag=false;
   string str="";

   while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
     {
      if(flag)
         FileReadNumber(file_handle);
      
      flag=true;
     }
  }


Thank you for reading.

 

Hello . You can save an array of a structure straight to the disk (as long as it does not contain objects from what i recall)

Try this : (i can explain any part if you want)

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
string system_folder="YourFolder",data_file="data_file.tdb";
int OnInit()
  {
//--- create timer
  // EventSetTimer(60);
  //Loading 
  MyTradesLog.Load(system_folder+"\\"+data_file);
  //test print load 
    string Text="Loaded "+IntegerToString(MyTradesLog.TRADES_Total)+"\n";
    for(int t=0;t<MyTradesLog.TRADES_Total;t++)
    {
    Text+="["+IntegerToString(t)+"].Ticket : "+IntegerToString(MyTradesLog.TRADES[t].MT_Ticket)+" ID : "+IntegerToString(MyTradesLog.TRADES[t].ID)+"\n";
    }
    Alert(Text);
  //test print load ends here 
  //Adding 
  MyTradesLog.Add(15456,62);
  //removing 
  MyTradesLog.Remove(0);
  //adding again to save 
  MyTradesLog.Add(134643,14);
  MyTradesLog.Add(256246,13);
  MyTradesLog.Save(system_folder+"\\"+data_file);
//---
   return(INIT_SUCCEEDED);
  }

struct my_trade_info
{
int MT_Ticket;
long ID;
my_trade_info(void){MT_Ticket=-1;ID=-1;}
};
struct trade_log
{
my_trade_info TRADES[];
int TRADES_Total,TRADES_Size,TRADES_Step;
trade_log(void){Reset();}
//reset
void Reset(){ArrayFree(TRADES);TRADES_Total=0;TRADES_Size=0;TRADES_Step=25;}
//load trade log 
void Load(string filename)
{
Reset();
  //if file exists ...
  if(FileIsExist(filename))
  {
  int fop=FileOpen(filename,FILE_READ|FILE_BIN);
    if(fop!=INVALID_HANDLE)
    {
    int k=(int)FileReadArray(fop,TRADES);
    TRADES_Total=k;
    TRADES_Size=ArraySize(TRADES);
    FileClose(fop);
    }
  }
  //if file exists ends here ...
}
//save trade log
void Save(string filename)
{
if(FileIsExist(filename)) FileDelete(filename);
if(TRADES_Total>0)
  {
  int fop=FileOpen(filename,FILE_WRITE|FILE_BIN);
  if(fop!=INVALID_HANDLE)
    {
    FileWriteArray(fop,TRADES,0,TRADES_Total);
    FileClose(fop);
    }
  }
}
//Add to trade log 
void Add(int ticket,long id)
{
TRADES_Total++;
if(TRADES_Total>TRADES_Size)
  {
  TRADES_Size+=TRADES_Step;
  ArrayResize(TRADES,TRADES_Size,0);
  }
TRADES[TRADES_Total-1].MT_Ticket=ticket;
TRADES[TRADES_Total-1].ID=id;
}
//Remove from trade log 
void Remove(int literal_adress)
{
  TRADES[literal_adress]=TRADES[TRADES_Total-1];
  TRADES_Total--;
}
};
trade_log MyTradesLog;
 
You write a CSV file. Open the file with your separator ('=') and read the ticket, and read the value.

Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help 2017.04.21

 
Lorentzos Roussos:

Hello . You can save an array of a structure straight to the disk (as long as it does not contain objects from what i recall)

Try this : (i can explain any part if you want)

Thank you again Lorentzos, thats a lot of work you did there and this seems like a good way to resolve my probleme.

Do you know if the data are saved for ever (until  I decide to erase them manually) or is there an expiration date (or maybe they get deleted when the EA is removed from the chart)

I see that it create a file in MT4 folders which seems to be a database file so I guess it's ok.


William Roeder:
You write a CSV file. Open the file with your separator ('=') and read the ticket, and read the value.

Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help 2017.04.21

Your sollution follows the code I wrote so far, and the csv structure is smart and easier to split datas with a delimiter.

Writing datas in the file is all good then and OrderTickets are separated from last_message_id perfectly with "=".


I won't code it today but to read values I was thinking about reading the 1st colunm with a while statement, until I find the OrderTicket that I'm looking for.

Then GET the number "x" of the line containing it.

That would leave me with the coordinate of  "last_message_id" (colunm 2, line x)

I would at last need to read the content of the cell to get what I need.


This seems like a very dirty way to do it, what do you think?

If you can point me to a cleaner way to make this happen that would be nice.

 
tdbguy:

Thank you again Lorentzos, thats a lot of work you did there and this seems like a good way to resolve my probleme.

Do you know if the data are saved for ever (until  I decide to erase them manually) or is there an expiration date (or maybe they get deleted when the EA is removed from the chart)

I see that it create a file in MT4 folders which seems to be a database file so I guess it's ok.

In the save function there is a deletion preceeding the actual save ,gated by if there are any trades .
Ideally , the data_file is named after the symbol the ea is running on.

Other than that if you build around it ,you will end up with a trade monitoring function with which you can control when the file gets erased (profit cycles etc)

 
Lorentzos Roussos:

In the save function there is a deletion preceeding the actual save ,gated by if there are any trades .
Ideally , the data_file is named after the symbol the ea is running on.

Other than that if you build around it ,you will end up with a trade monitoring function with which you can control when the file gets erased (profit cycles etc)

Alright this is working perfectly fine, thank you Lorentzos for your time.You saved mine a lot!

I'm creating a unique file that stores trade's datas each time, which I then delete when the Trade is closed, this is easier to search and collect datas; Of course it creates a little delay but its ok.


Do you know how to delete a file within a folder? with your code I did :

  string system_folder="Telegram - MT4",data_file=OrderTicket()+".tdb";

But FileDelete(data_file); does not seems intented for sub folders...

I tried FileDelete(data_file,system_folder); without success.

 
tdbguy:

Alright this is working perfectly fine, thank you Lorentzos for your time.You saved mine a lot!

I'm creating a unique file that stores trade's datas each time, which I then delete when the Trade is closed, this is easier to search and collect datas; Of course it creates a little delay but its ok.


Do you know how to delete a file within a folder? with your code I did :

But FileDelete(data_file); does not seems intented for sub folders...

I tried FileDelete(data_file,system_folder); without success.

Yes use FileDelete(system_folder+"\\"+data_file);