[MQL4 Coding Help] Overwrite .xml file periodically

shanmugapradeep

Corporal
Messages
86
Hello,

I am fetching news from http://nfs.faireconomy.media/ff_calendar_thisweek.xml and it save the file inside terminal/MQL4\Files as FFCPing(Symbol)-ffcal_week_this.xml (eg, FFCPingGBPUSD-ffcal_week_this.xml). I have added a code to check this file every 2 hours and overwrite it to new version.



Code:
int OnInit()
  {
//--- get today time
   TimeOfDay=(int)TimeLocal()%86400;
   Midnight=TimeLocal()-TimeOfDay;
//--- set xml file name ffcal_week_this (fixed name)
   xmlFileName=INAME+"-ffcal_week_this.xml";
//--- checks the existence of the file.
   if(!FileIsExist(xmlFileName))
     {
      xmlDownload();
      xmlRead();
     }
//--- else just read it
   else
      xmlRead();
//--- get last modification time
   xmlModifed=(datetime)FileGetInteger(xmlFileName,FILE_MODIFY_DATE,false);
//--- check for updates
   if(!FileIsExist(xmlFileName))
     {
      if(xmlModifed<TimeLocal()-(2*3600))
        {
         Print(INAME+": xml file is out of date");
         xmlUpdate();
        }
      //--- set timer to update old xml file every x hours

      EventSetTimer(2*3600);
     }
   assignVal=true;
  return(INIT_SUCCEEDED);
}

void OnTimer()
  {
//---
   assignVal=true;
   Print(INAME+": xml file is out of date");
   xmlUpdate();
//---
  }


void xmlDownload()
  {
   Sleep(3000);
//---
   ResetLastError();
  

   string cookie=NULL, headers;
   string reqheaders="User-Agent: Mozilla/4.0\r\n";
   char post[],result[];
   int res;
   string url="http://nfs.faireconomy.media/ff_calendar_thisweek.xml";
   ResetLastError();
   int timeout=5000;
   res=WebRequest("GET",url,reqheaders,timeout,post,result,headers);
   if(res==-1)
     {
      Print("Error in WebRequest. Error code  =",GetLastError());
      //--- Perhaps the URL is not listed, display a message about the necessity to add the address
      MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
     }
   else
     {
      //--- Load successfully
      PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
      //--- Save the data to a file
      int filehandle=FileOpen(xmlFileName,FILE_WRITE|FILE_BIN);
      //--- Checking errors
      if(filehandle!=INVALID_HANDLE)
        {
         //--- Save the contents of the result[] array to a file
         FileWriteArray(filehandle,result,0,ArraySize(result));
         //--- Close the file
         FileClose(filehandle);
        }
      else
         Print("Error in FileOpen. Error code=",GetLastError());
     }
//---
  }


Error: It not overwriting automatically. I have to delete the file manually and than this code create new .xml file. How to fix this?. How to make this overwrite existing file automatically?
 
use FILE_WRITE | FILE_BIN | FILE_COMMON in the FileOpen function.

int filehandle = FileOpen(xmlFileName, FILE_WRITE | FILE_BIN | FILE_COMMON);
 
Back
Top