跳至内容

FileFlush

FileFlush

将输入/输出文件缓冲区中剩余的所有数据写入磁盘。

void  FileFlush(
   int  file_handle      // File handle
   );

参数

file_handle

[in] 由 FileOpen() 返回的文件描述符。

返回值

不返回任何值。

注意

当向文件写入数据时,数据可能实际上只有在一段时间后才会出现在文件中。要立即将数据保存到文件中,请使用 FileFlush() 函数。如果不使用此函数,尚未存储到磁盘的部分数据将在使用 FileClose() 关闭文件时才会被强制写入磁盘。

当写入的数据具有特定值时,应使用此函数。需要注意的是,频繁调用该函数可能会影响程序的运行速度。

在从文件读取和写入数据之间,必须调用 FileFlush() 函数。

示例:

//--- 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());
  }

另请参阅

FileClose()

最后更新于