//+------------------------------------------------------------------+ //| Demo_FileFlush.mq5 | //| Copyright 2013, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2013, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" //--- show the window of input parameters when launching the script #property script_show_inputs //--- file name for writing input string InpFileName="example.csv"; // file name //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- reset error value ResetLastError(); //--- open the file int file_handle=FileOpen(InpFileName,FILE_READ|FILE_WRITE|FILE_CSV); if(file_handle!=INVALID_HANDLE) { //--- write data to the file for(int i=0;i<1000;i++) { //--- call write function FileWrite(file_handle,TimeCurrent(),SymbolInfoDouble(Symbol(),SYMBOL_BID),SymbolInfoDouble(Symbol(),SYMBOL_ASK)); //--- save data on the disk at each 128th iteration if((i & 127)==127) { //--- now, data will be located in the file and will not be lost in case of a critical error FileFlush(file_handle); PrintFormat("i = %d, OK",i); } //--- 0.01 second pause Sleep(10); } //--- close the file FileClose(file_handle); } else PrintFormat("Error, code = %d",GetLastError()); } //+------------------------------------------------------------------+